Automating Image Manipulation with GD - Page 14
October 22, 2001
|
This is the first article in a series that will explore concepts
and techniques that can automate many image manipulation tasks.
As with the last series, the goal of this series is to reduce the
amount of work that you do on a regular basis as a developer,
administrator, or Webmaster.
|
The GD library
We will be utilizing the GD image manipulation library,
which was written in C by Thomas Boutell. The Perl adaptation was
written by Lincoln Stein.
The GD library is capable of drawing primitive shapes such as
circles, squares, and lines. It's also capable of drawing text
strings, as well as performing image manipulation such as
combining and resizing images. The library can read and write
JPEG, PNG, and XPM graphic formats. It supported GIF until
Unisys, who owns the patent for the format, decided to start
charging for the format's use. Because of that incident, Thomas
Boutell decided to remove GIF support from the library.
Drawing borders
The first thing we'll learn to do is draw several different types
of borders around an existing image. These exercises will help
you become more familiar with the GD library syntax.
Drawing a 1-pixel border with rectangle()
In this first example, we will draw a black border around an
image. With the GD library, we do this with the
rectangle() function, which draws a rectangle given
the starting x and y coordinates, the ending x and y coordinates,
and the color of the border.
1 use GD;
2 my $image = GD::Image->newFromJpeg("66044.jpg");
3 my $black = $image->colorResolve(0,0,0);
4 my ($width,$height) = $image->getBounds();
5 $image->rectangle(0,0,($width-1),($height-1),$black);
6 open(FILE, ">border1.jpg") || die "Cannot open border1.jpg: $!\n";
7 print FILE $image->jpeg;
The first line loads the JPEG file 66044.jpg into a GD object.
The second line allocates an index in the color map for black.
Images can contain a maximum of 256 colors, so when modifying an
existing image, you will either need to create a new mapping for
a color, use an existing color in the map, or delete an existing
color to make room for the new color. The
colorResolve() function on the second line looks for
the existence of a color in the color map (0,0,0 in this case
which are the red, green, and blue values). If the color does not
exist in the map, it will create it if there is room. You can
check the number of colors in the map by calling the
colorsTotal() function. The getBounds()
function returns the width and height of the image that was just
loaded. These values will be used to calculate the coordinates
for the rectangle that is drawn on the next line.
The beginning coordinates for an image are 0,0. To draw the
border, we call the rectangle() function. The first
two arguments are the x,y coordinates where the rectangle will be
drawn from. The second two parameters specify the ending two
coordinates for the rectangle. The last parameter is the color
map index for the color of the rectangle. The rectangle will be 1
pixel wide. The last two lines of the script write the new image
with the border out to the border1.jpg file.
Drawing wide borders
Since the border is only 1 pixel wide, it may be a bit narrow to
be noticeable. We can however draw a wider border by drawing
multiple rectangles next to each other. The for loop below draws
three consecutive rectangles, one inside of the other from the
outer edge of the image.
1 use GD;
2 my $image = GD::Image->newFromJpeg("66044.jpg");
3 my $black = $image->colorResolve(0,0,0);
4 my ($width,$height) = $image->getBounds();
5 for (my $i=0; $i <= 3; $i++) {
6 $image->rectangle($i,$i,($width-1-$i),($height-1-$i),$black);
7 }
8 open(FILE, ">border2.jpg") || die "Cannot open border2.jpg: $!\n";
9 print FILE $image->jpeg;
The difference between the previous example and this one is the
for loop on lines 5 through 7. The loop iterates 3 times, drawing
a black rectangle one pixel wide in each direction for each
iteration.
Processing URLs - Page 13
Weaving Magic With Regular Expressions
Using a Style to Draw a Dotted Line - Page 15
|