TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Advertisement
The Big GD Guide - Part 1
   What is GD?

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
Any many more.

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(11020 


Properties


PHP Code:
imagecreate     int width    int height    
This will create a plain image 200 width and 200 height. So in this case a square. You are able to add to this like using strings. This can be done using a simple function.

Strings

PHP Code:
imagestring($im155,  "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    
image
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"); 
Now this means that you cannot place any other PHP code or HTML on this page. Only GD based code. This will of course display the actual image when you use the following script, but any code like "echo" or HTML code will cause errors.

Displaying the GD results

PHP Code:
    imagejpeg ($image); 
Properties

$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); 
The code above allows you to create from a jpeg, but also allows you to create from other file types.
True Colour

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$image0000$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); 
Properties

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.
Report this Article
Last 5 Article Reviews Read All Reviews
   Good Info
Review added by pabin on 02-24-2008
Thanks for the GD article. It really has helped a lot.
   Once again, good stuff.
Review added by ReSpawN on 01-28-2008
As the title might explain, once again you've helped me out a lot Dale! Keep up the good work and I'm also looking forward to Part II!
   Nice article
Review added by Nor on 01-24-2008
Helped me a bunch, GD is one hard thing to remember in PHP, this helped me a lot. Waiting for your next part.

All times are GMT. The time now is 07:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design