Creating/Manipulating Images with gd

 in
Use the gd graphics code library to create and manipulate images quickly.

gd is an open-source library that allows users to create and manipulate images easily. It lets you open images in formats such as JPEG, PNG, XPM and a few more. gd works something like this: it opens images in different formats and converts them to generic bit-mapped images in memory. It then lets you do graphical operations, such as drawing lines, arcs, ellipses or rectangles on that image, and stores the resulting image in any of the earlier-mentioned formats. For example, you could write a simple command-line program that converts a given file in JPEG format to PNG using gd. gd also can change colors in the image and copy, cut, merge or rotate it.

In addition, gd is useful when you want to create images on the fly. With gd, you programmatically can create an image, color it, draw on it and save it to disk.

gd is best known for creating images on the fly for use in Web pages. This is made possible with the help of PHP.

If you have a GNU/Linux system that uses RPM to manage packages, run rpm -q gd to find out if gd is installed already. If not, you can download the latest tarball from here.

Creating Images

The following program creates a 100x100 pixel black image with a white line running diagonally across it, as shown here:


/* File : gd-eg1.c */
#include < gd.h >
#include < stdio.h >

int main() {
	gdImagePtr im; //declaration of the image
	FILE *out; //output file
	int black,white;

	im = gdImageCreate(100,100); //create an image, 100
by 100 pixels

	black = gdImageColorAllocate(im, 0, 0, 0); //
allocate black color
	white = gdImageColorAllocate(im, 255, 255, 255);	//
allocate white color	
	gdImageLine(im, 0, 0,100,100, white); // draw a line
using the allocated white color.

	out = fopen("test.jpg", "w"); //open a file
	gdImageJpeg(im, out, -1); //write the image to the
file using the default quality setting

	/* be good, clean up stuff */
	fclose(out); 
	gdImageDestroy(im);
}

Compile the program with the following command:


$ gcc gd-eg1.c -lgd

Run the resulting a.out file, and a test.jpg file will be created in the current directory. If you view it, you should see a 100x100 pixel black image with a white line cutting across. The program is pretty simple, but I'll explain the code a little.


	gdImagePtr im; //declaration of the image

declares a pointer to a gd image descriptor.

	

	im = gdImageCreate(100,100); //create an image, 
100 by 100 pixels

creates an image 100x100 pixels in size and stores the reference it returns in the variable im. This is much like a file handle. All further operations on this image are carried out using this reference.

	

	black = gdImageColorAllocate(im, 0, 0, 0); //
allocate black color
	white = gdImageColorAllocate(im, 255, 255, 255);	//
allocate white color

Before you can draw anything on the image, you need to allocate color. Allocating color for the first time in a newly created image makes it the background color for that image. The function gdImageColorAllocate takes four arguments. The first one is the image pointer and the next three are Red, Green and Blue values, respectively. Thus, calling gdImageColorAllocate(im, 0, 0, 0) for the newly created image paints the background of the new image black. We store the color indexes in variables, because graphical drawing or font drawing functions take a "color" argument.

	

	gdImageLine(im, 0, 0,100,100, white); // draw a line using the 
allocated white color.

This function draws a line from the top left corner (0,0) to the bottom right corner (100,100) using the color white on the image pointed to by im.


	gdImageJpeg(im, out, -1); //write the image to the file using the 
default quality setting

This is the function call that writes the image to a disk file in JPEG format. The final argument of this function is the quality setting for JPEG format images. This can be anything between 1 and 100, where 100 is the highest quality. Passing -1 uses the default quality setting. Similarly, there are other functions that store images in different formats:


	GdImagePng(im,out) // store as PNG (note no quality setting)

	GdImageGd and gdImageGd2 are functions that store images in formats 
specified by the library.

	gdImageDestroy(im);

Finally, you release memory allocated to hold the image data.

Note that the PNG format enjoys good support and uses better compression algorithms. It also achieves something that the JPEG format does not--transparency. GIF format images, although good enough, use the LZW compression algorithm patented by Unisys when using full compression. GIF format support in gd thus was dropped. And you must have read about the hue and cry against software patents; more on this here.

______________________