Create images in php with GD Library

Php gives us one more powerful library known as GD Library to work with images. We can create dynamic images, write text on images and so on. It gives us a lot of functions to manipulate our images.

It is very useful when we create captcha or watermark images.

To work with GD Library it must be installed on your server.

Creating an image in php requires four steps :

 (1) Create a blank image canvas for php to work with. This is an area of the web server's memory that is set aside fot drawing onto.

(2) Work through the steps involved in drawing the image as per your need.

(3) Send your finished images to the browser or save it to disk.

(4) Remove your image from the server's memory.

Create a new image
To create a blank image canvas we can use either
imagecreate() or imagecreatetruecolor() function.

Ex:  $Im = imagecreate( 250, 200);

Allocate colors to image
Ex:  imagecolorallocate( $Im, 41, 143, 120 );

Outputting images
Once you have an image in memory, Its time to output it? We can do it using one of three funtions:

imagejpeg() outputs the image in JPEG format
imagegif() outputs the image in GIF format
imagepng() outputs the image in PNG format

Ex:  imagejpeg( $Im , "image.jpeg");

If you want to send the image directly to the browser, send an appropriate HTTP header so that the browser knows how to handle the image and omit the second parameter of above function. Ex:

header("Content-type: image/jpeg");
imagejpeg( $Im , "image.jpeg");

Remove image from servers memory.

Example code to create a blank image

  $Im = imagecreate( 250, 200);
  imagecolorallocate( $Im, 41, 143, 120 );
  header("Content-type: image/jpeg");

As you have already seen, to create a new image from scratch you use the imagecreate() or imagecreatetruecolor() function.

To create a new image based on an existing image you use the imagecreatefrom... series functions. The most common of these are imagecreatefromjpeg(), imagecreatefromgif() and imagecreatefrompng().

The imagecreatefromjpeg() function works the same way as the imagecreate() function, except instead of passing it the width and height, you only pass the filename of the existing image as a string. The functions returns an image resource with which you can work .

For Example :

Sample code to create an image with existing image

  $Im = imagecreatefromjpeg("myimage.jpg");
  header("Content-type: image/jpeg");

Now let us add some text to image.

Adding text to images enables you to draw dynamic charts and graphs. The quickest and easiest way to add text to an image is to use the imagestring() funtion, which lets you to draw a string of text at the location you specify. imagestring() takes six parameters as follows.

The image resource, the font to use for the text, the x axis, the y axis, the text string you want to draw, the color of the text.

imagestring() returns true if it successfully added the text to the image, or false if there was some error.

Sample code to write text on images

  $Im = imagecreate( 120, 40);
  $color=imagecolorallocate( $Im, 41, 143, 120 );
  $white=imagecolorallocate( $Im, 255, 255, 255 );
  imagestring( $Im, 5, 5, 12, "This is text", $white);
  header("Content-type: image/jpeg");

You can also write text on an existing image. Just create an image with an existing image (discussed above) and use imagestring() function.

Now you know how to create images, write text on images.

Your may want to know :

How to create a simple captcha script in php.
Create images in php with GD Library Create images in php with GD Library Reviewed by Altaf Hussain on March 26, 2012 Rating: 5

No comments:

Altaf Web. Powered by Blogger.