The GD library is a way for PHP users to create and edit images on the web. The GD library has many different functions that can be used in many different ways. Below are some examples of how the GD library is used.
Graphs
GD is used to create graphs that can be shown on the website. These graphs can be dynamically updated with a connection to a MySQL database. Such forms that can be created are.
- Pie Charts
- Line Charts
- Bar Charts
- Scatter Charts
This is only a few of them that can be made, but if you know how to use the library well the possibilities are endless.
Thumbnails
You are also able to create thumbnails for your website. If you do not wish to sit there and create a thumbnail for every image you wish on your website using a image editing software like Photoshop or Fireworks. Then the GD library is a good idea for you to learn. You are able to resize images to a certain size say 150 x 150 or even keep the image ratio so the image is not pushed and pulled all over the place. You are also able to control things like the Zoom or how much of the image is actually shown in the newly created thumbnail.
Captcha ( Security Images )
Security images are all over the internet now a days to protect websites owners from bots and spiders that can fill out forms and then post information on the website mainly advertisements that are not wanted. These Captcha images can stop this happening by creating a image usually around 150 x 80 depending on how many numbers and letters or complex you wish to have it. This forces the user to actually copy this information that is dynamically on a image created using the GD library which is pretty hard to get around if done correctly.
How are these used?
These are used to create many different web based applications that would otherwise be hard to create in a short amount of time. Some examples are[/color][/size]
- Image galleries
- Contact Forms / Application Forms
- Forums
- News Systems
The Codes
Now we can start of by using some of the most used codes in the GD library. We can create a basic image totally blank with nothing on it with one simple line of code.
Creating a empty image
PHP Code:
$im = imagecreate(110, 20
Properties
PHP Code:
imagecreate ( int width , int height )
Strings
PHP Code:
imagestring($im, 1, 5, 5, "Hello world", $text_color);
Example
http://uk2.php.net/manual/en/figures...magestring.png
Properties
PHP Code:
imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor() or in this case imagecreate()
font
Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont().
x - x-coordinate of the upper left corner
y - y-coordinate of the upper left corner
string -The string to be written
color - A color identifier created with imagecolorallocate()
Outputting the results
Now outputting the results there are things you need to take into account. You must define a header type and set it to a image type. Like so
PHP Code:
header("Content-type: image/png");
Displaying the GD results
PHP Code:
imagejpeg ($image);
$image- This being the actual image that created the image so the use of imagecreate($width, $height);
Clearing the memory
You also want to clear the memory of the information of the image used. This wont get rid of all the information and not display. It will still display, but clean up some memory used by things that just ain't needed anymore.
We can do this by doing the following.
PHP Code:
imagedestroy($image);
Properties
$image - the actual variable that stored the created image using a function like imagecreate($width, $height);
Creating from an image
Using the GD library you are also able to actually create images from a pre-existing image. This is the method used in creating thumbnails. This allows you to copy a image and returns an image identifier representing the image obtained from the given filename.
PHP Code:
$src_file = "http://www.talkphp.com/images/image.jpg";
imagecreatefromjpeg($src_file);
- imagecreatefromgif
- imagecreatefromjpeg
- imagecreatefrompng
- imagecreatefromstring
- imagecreatefromwbmp
- imagecreatefromxbm
- imagecreatefromxpm
You are able to returns an image identifier representing a black image of the specified size using the true colour function. This will be used in conjunction with the imagecreatefromjpeg() function or other createfrom functions. Once this has been done you can then copy a rectangular portion of one image (in this case the createfrom() function) to another image (truecolor function), smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.
This can be done using the following codes.
PHP Code:
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Properties - imagecreatetruecolor
$new_width - The actual new width you wish to create for say a thumbnnail
$new_height - The new height you wish to create for say a thumbnail
Properties - imagecopyresampled
PHP Code:
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
dst_im - Destination image link resource (In this case the true color function)
src_im - Source image link resource (In this case the createfrom function)
dst_x - x-coordinate of destination point
dst_y - y-coordinate of destination point
src_x - x-coordinate of source point
src_y - y-coordinate of source point
dst_w - Destination width
dst_h - Destination height
src_w - Source width
src_h - Source height
Dealing with colour
You are able to create colours using the GD library these can then be assigned to pretty much anything like strings, backgrounds, shapes and so on.
We can use a simple line of code to create one colour using the RGB properties of colours.
PHP Code:
$white = ImageColorAllocate($background,255,255,255);
background - This variable is linked with the imagecreatetruecolor() function we used above
255 - You can see there are 3 lots of 255 this has set the RGB properties to the colour white in Hexadecimal
How do i know what the codes are?
You can find them on all image editing software when you open the colour pallet or you can search the internet. Here is a link to one of them that contains up to 500 different colours.
500+ Named Colours with rgb and hex values
All this information can be put together and create some nice effects and save you a lot of time when dealing with images on the internet.
Please keep coming back to view part two of this big guide to GD library. Next we shall talk about more advance functions including transparent, alpha blending and so on.


Join the friendly bunch on IRC...