Image::Imlib2 - Interface to the Imlib2 image library

      use Image::Imlib2;

      # create a new image
      my $image = Image::Imlib2->new(200, 200);

      # Enable the alpha channel support

      # set a colour (rgba, so this is transparent orange)
      $image->set_color(255, 127, 0, 127);

      # draw a rectangle
      $image->draw_rectangle(50, 50, 50, 50);

      # draw a filled rectangle
      $image->fill_rectangle(150, 50, 50, 50);

      # draw a line
      $image->draw_line(0, 0, 200, 50);

      # set quality before saving

      # save out

      # create a polygon
      my $poly = Image::Imlib2::Polygon->new();

      # add some points
      $poly->add_point(0, 0);
      $poly->add_point(100, 0);
      $poly->add_point(100, 100);
      $poly->add_point(0, 100);

      # fill the polygon

      # draw it closed on image
      $image->draw_polygon($poly, 1);

      # create a color range
      my $cr = Image::Imlib2::ColorRange->new();

      # add a color
      my ($distance, $red, $green, $blue, $alpha) = (15, 200, 100, 50, 20);
      $cr->add_color($distance, $red, $green, $blue, $alpha);

      # draw it
      my($x, $y, $width, $height, $angle) = (20, 30, 200, 200, 1);
      $image->fill_color_range_rectangle($cr, $x, $y,
                                         $width, $height, $angle);

    Image::Imlib2 is a Perl port of Imlib2, a graphics library that does
    image file loading and saving as well as manipulation, arbitrary polygon
    support, etc. It does ALL of these operations FAST. It allows you to
    create colour images using a large number of graphics primitives, and
    output the images in a range of formats.

    Image::Imlib2::Polygon and Image::Imlib2::ColorRange are described
    following Image::Imlib2 but may be referenced before their description.

    Note that this is an early version of my attempt at a Perl interface to
    Imlib2. Currently, the API is just to test things out. Not everything is
    supported, but a great deal of functionality already exists. If you
    think the API can be tweaked to be a bit more intuitive, drop me a line!

    Note that a development version of Imlib2 must be installed before
    installing this module.

Exported constants

    To be used as the direction parameter for text functions that accept it.

METHODS (Image::Imlib2)
    This will create a new, blank image. If the dimensions aren't specified,
    it will default to 256 x 256.

      my $image = new Image::Imlib2->new(100, 100)

    This will load an existing graphics file and create a new image object.
    It reads quite a few different image formats.

      my $image = Image::Imlib2->load("foo.png");

    This saves the current image out. Currently this is in PNG if the format
    has not been set using image_set_format().


  image_set_format (format)
    This will set the image format for future save operations. format is a
    string and may be "jpeg", "tiff", "png", etc. The exact number of
    formats supported depends on how you built imlib2.

      $image->image_set_format("jpeg"); # Convert image to JPG

    This sets the quality of the saved picture - lower the quality to get
    smaller filesizes.


  set_color (r, g, b, a) or set_colour (r, g, b, a)
    This sets the colour that the drawing primitives will use. You specify
    the red, green, blue and alpha components, which should all range from 0
    to 255. The alpha component specified how transparent the colour is: 0
    is fully transparent (so drawing with it will be pointless), 127 is
    half-transparent, and 255 is fully opaque. Many examples:

      $image->set_colour(255, 255, 255, 255); # white
      $image->set_colour(  0,   0,   0, 255); # black
      $image->set_colour(127, 127, 127, 255); # 50% gray
      $image->set_colour(255,   0,   0, 255); # red
      $image->set_colour(  0, 255,   0, 255); # green
      $image->set_colour(  0,   0, 255, 255); # blue
      $image->set_colour(255, 127,   0, 127); # transparent orange

  draw_point (x, y)
    This colours a point in the image in the currently-selected colour. Note
    that the coordinate system used has (0, 0) at the top left, with (50, 0)
    to the right of the top left, (0, 50) below the top left, and (50, 50)
    to the bottom right of the top left.

      $image->draw_point(50, 50);

  query_pixel (x, y)
    This returns the colour of a pixel in the image. It returns the red,
    green, blue and alpha components:

      my($r, $g, $b, $a) = $image->query_pixel(50,50);

  draw_line (x1, y1, x2, y2)
    This draws a line between two points in the currently-selected colour.
    The following draws between the (0, 0) and (100, 100) points:

      $image->draw_line(0, 0, 100, 100);

  draw_rectangle (x, y, w, h)
    This draws a the outline of a rectangle with the top left point at (x,
    y) and having width w and height h in the current colour.

      $image->draw_rectangle(0, 0, 50, 50);

  fill_rectangle (x, y, w, h)
    This draws a filled rectangle with the top left point at (x, y) and
    having width w and height h in the current colour.

      $image->fill_rectangle(0, 0, 50, 50);

  draw_ellipse (x, y, w, h)
    This draws an ellipse which has center (x, y) and horizontal amplitude
    of w and vertical amplitude of h in the current colour. Note that
    setting w and h to the same value will draw a circle.

      $image->draw_ellipse(100, 100, 50, 50);

  fill_ellipse (x, y, w, h)
    This draws a filled ellipse which has center (x, y) and horizontal
    amplitude of w and vertical amplitude of h in the current colour. Note
    that setting w and h to the same value will draw a filled circle.

      $image->fill_ellipse(100, 100, 50, 50);

  add_font_path (dir)
    This function adds the directory path to the end of the current list of
    directories to scan for truetype (TTF) fonts.


  load_font (font)
    This function will load a truetype font from the first directory in the
    font path that contains that font. The font name format is
    "font_name/size". For example. If there is a font file called cinema.ttf
    somewhere in the font path you might use "cinema/20" to load a 20 pixel
    sized font of cinema.

    Note that this font will be used from now on, much like set_colour does
    for colours.


  get_font_size (text, direction, angle)
    This function returns the width and height in pixels the text string
    would use up if drawn with the current font. direction and angle are
    optional and deault to TEXT_TO_RIGHT and 0, respectively.

      my($w, $h) = $image->get_text_size("Imlib2 and Perl!");
      my($w1, $w2) = $image->get_text_size("Crazy text",
                                           TEXT_TO_UP, 1);

  draw_text (x, y, text, direction, angle)
    This draws the text using the current font and colour onto the image at
    position (x, y). direction and angle are optional and deault to
    TEXT_TO_RIGHT and 0, respectively.

      $image->draw_text(50, 50, "Groovy, baby, yeah!");
      $image->draw_text(50, 50, "Sweet, baby, yeah!",
                        TEXT_TO_UP, 1.571);

  crop (x, y, w, h)
    This creates a duplicate of a x, y, width, height rectangle in the
    current image and returns another image.

      my $cropped_image = $image->crop(0, 0, 50, 50);

  blend (source_image, merge_alpha, sx, sy, sw, sh, dx, dy, dw, dh)
    This will blend the source rectangle x, y, width, height from the
    source_image onto the current image at the destination x, y location
    scaled to the width and height specified. If merge_alpha is set to 1 it
    will also modify the destination image alpha channel, otherwise the
    destination alpha channel is left untouched.

      $image->blend($cropped_image, 0, 0, 0, 50, 50, 200, 0, 50, 50);

  blur (radius)
    This will blur the image. A radius of 0 has no effect, 1 and above
    determine the blur matrix radius that determine how much to blur the


  sharpen (radius)
    This sharpens the image. The radius affects how much to sharpen by.

  clone ()
    This creates an exact duplicate of the current image.

      $cloned = $image->clone;   

  draw_polygon (polygon, closed)
    This will draw polygon (of type Imlib2::Image::Polygon) on the the
    image. The the polygon is drawn closed is closed is 1 and open if closed
    is 0.

      $image->draw_polygon($poly, 1);

  fill_color_range_rectangle(color_range, x, y, w, h, angle);
    This uses the color range color_range to fille a rectangle with points
    x, y, x+width, y+width.

      $image->fill_color_range_rectangle($cr, 10, 20, 100, 150, 0);

  image_orientate (steps)
    This will rotate the image by steps*90 degrees, so to rotate by 90
    degrees set to 1, for 180 degrees set to 2, etc.

      $image->image_orientate(1);                    # Rotate by 90 degrees.

  create_scaled_image (x, y)
    Create a new image, scaled from the original to the dimensions given in
    x and y. If x or y are 0, then retain the aspect ratio given in the

      $image2=$image->create_scaled_image(100,100);  # Scale to 100x100 pixels

  flip_horizontal ()
    This will flip/mirror the image horizontally.


  flip_vertical ()
    This will flip/mirror the image vertically.


  flip_diagonal ()
    This will flip/mirror the current image diagonally (good for quick and
    dirty 90 degree rotations if used before to after a horizontal or
    vertical flip).


  has_alpha (BOOLEAN)
    Queries and/or sets the alpha support flag for the image. Note that
    alpha is on by default when you create an image:

       if ($image->has_alpha) {
          # do something requiring alpha support
       # Enable the alpha channel

  set_cache_size (INT)
    By default, Imlib2 will cache all loaded images (up to some maximum
    cache size) and will use this cache to avoid loading images from disk.

    Sets the size of the image cache. Reducing this value will cause the
    cache to be emptied. You can turn off caching all together by setting
    this to zero.

    Even without a cache, as long as you have a reference to an image in
    memory that image will be returned immediately without checking the

      my $image = Image::Imlib2->load("foo.jpg"); # image loaded from disk
      ... later, somewhere else, after $image has gone away ...
      my $image = Image::Imlib2->load("foo.jpg"); # same image, even if changed on disk
      ... later, somewhere else, after $image has gone away ...
      my $image  = Image::Imlib2->load("foo.jpg"); # image loaded from disk  
      my $image2 = Image::Imlib2->load("foo.jpg"); # same image as before, not reloaded 

  get_cache_size ()
    Returns the maximum size of the Image cache.

  set_changes_on_disk ()
    Called on an Image::Imlib2 instance that you have loaded from disk, this
    method tells imlib that it should take extra care when caching the image
    for this filename. Next time the load method is called for this image's
    file name Imlib will check the modification time for the file on disk
    compared to the cached version and take appropriate action.

      my $image = Image::Imlib2->load("foo.jpg");


      # reloads image from disk if mod time has changed (otherwise use cached)
      my $image = Image::Imlib2->load("foo.jpg"); 

    Calling this method on a loaded image tells Imlib2 to look at the disk
    and compare mtimes with it's loaded copy - by default, this is not the
    case, so even if a file changes on disk, it won't be re-loaded.

METHODS (Image::Imlib2::Polygon)
    This will create a new polygon for use with Image::Imlib2::draw_polygon.

      my $poly = Image::Imlib2::Polygon->new();

  add_point (x, y)
    Adds a point to the polygonal construct.


    Fills polygon in the current context.


METHODS (Image::Imlib2::ColorRange)
    Creates a new color range.

      my $cr = Image::Imlib2::ColorRange->new();

  add_color (distance, red, green, blue, alpha)
    Similar to set_colour, but adds the color to the color range at the
    specified distance.

      $cr->add_color(10, 255, 127, 0, 66);

    Returns the current width of the image.

      my $width = $image->get_width;

    Returns the current height of the image.

      my $height = $image->get_height;

    Leon Brocard,

    Copyright (c) 2000-5 Leon Brocard. All rights reserved. This program is
    free software; you can redistribute it and/or modify it under the same
    terms as Perl itself.