
- Drop me a line! Contact me here.
Starting Out with GD
The caption for our image that we will be using is "A girl". Pretty descriptive, huh?
Now we have calculated where to place the text we need to write it to the image.
Now we've completed our image.

To output the image to a web browser we need to send the correct mime header, and the image data complied into an image file. For most photographic type images we should use the JPG file format as this is better designed for compressed that sort of data.
In this case though we are best off using the PNG file format as it is better at handling small graphics with limited palettes. The mime type for a PNG image is image/png, so we send that using the header function first. Then we send the image data with the imagepng() function.
The imagepng() function also has the option to save the image if required. This is useful if you want to generate an image that will be used over and over again as you can generate the image using an administration form, and then display it on the front end of your web site as an ordinary web graphic.
Finally there is one last step to creating a graphic file on the fly, and that is to close all the image resources at the end of your script. This is good house keeping really, and while it isn't strictly necessary it is definitely a good idea. If your script were to malfunction it might leave things behind when it closes and take up valuable memory on the server.
There is considerably more functionality to the GD library and its associated PHP functions. Have a play, its great fun.
<?php
$caption = "A girl";
$fontwidth = imagefontwidth(3);
$txtwidth = strlen($caption)*$fontwidth;
$txtx = ((200-$txtwidth)/2);
?>
Now we have calculated where to place the text we need to write it to the image.
<?php
imagestring($image,3,$txtx,180,$caption,$red);
?>
Now we've completed our image.

To output the image to a web browser we need to send the correct mime header, and the image data complied into an image file. For most photographic type images we should use the JPG file format as this is better designed for compressed that sort of data.
In this case though we are best off using the PNG file format as it is better at handling small graphics with limited palettes. The mime type for a PNG image is image/png, so we send that using the header function first. Then we send the image data with the imagepng() function.
<?php
header("Content-type: image/png");
imagepng($image);
?>
The imagepng() function also has the option to save the image if required. This is useful if you want to generate an image that will be used over and over again as you can generate the image using an administration form, and then display it on the front end of your web site as an ordinary web graphic.
Finally there is one last step to creating a graphic file on the fly, and that is to close all the image resources at the end of your script. This is good house keeping really, and while it isn't strictly necessary it is definitely a good idea. If your script were to malfunction it might leave things behind when it closes and take up valuable memory on the server.
<?php
imageclose($girl);
imageclose($image);
?>
There is considerably more functionality to the GD library and its associated PHP functions. Have a play, its great fun.
Comments
Comments are not currently being accepted for this article.
Sidebar
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 9
- DB Time: 0.1484 seconds
