PHP scripts can generate images using the GD library. This is a built in extension but is not enabled by default. To enable it in the make sure the line
or equivalent unix shared library is present in the php.ini file. The PHP manual 35 gives details on the use of the GD library with PHP. The PHP functions wrap the C-based GD library. More information on the GD project can be found at the libgd site 71.
Here is a program that creates a simple rectangle and then outputs it to the browser as a PNG image.
The program first creates an image object with the ImageCreate($width, $height) call. Then it allocates
objects for the colors white and red with the function ImageColorAllocate($image, $r, $g, $b). A red rectangle
is drawn on the image with the call ImageFilledRectangle($image, $x1, $y1, $x2, $y2, $color). All the units
are pixels and the coordinate origin is at the top left. A header is written to let the browswer know to expect a
PNG MIME type. Finally, the image is written to the browswer with the call ImagePNG($image).
The image generated looks like this.
Open rectangles can be drawn with the ImageRectangle($image, $x1, $y1, $x2, $y2, $color) function and lines
can be drawn with the function ImageLine($image, $x1, $y1, $x2, $y2, $color). This is demonstrated in
the script test_gd_line.phps. The program output is shown below:
You can draw text on images with the ImageString($image, $fontSize, $x, $y, $text, $color) function.
The program test_gd_text.phps demonstrates this. The image generated is
shown below.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.