Monday 12 March 2012

PHP GD Library

·     php is not limited to creating just HTML output. It can also be used to create and manipulate image files in different image formats.
·         php can also output image streams directly to a browser.
·         php provides a feature to draw images using various kinds of shapes.
·         The area where the shapes are drawn is known as canvas.
·         php GD (Graphical Display) Library is used to draw such images.
·     GD is an open source code library for the dynamic creation of images by programmers. GD is written in C, and "wrappers" are available for Perl, PHP and other languages. 
·         Different functions available in the GD Library are used to create and manipulate the images and place the image on the browser.
·         There are different versions of GD in php
1)     GD 1.6 – It supports GIF images and not PNG
2)     GD 2.0.28 – It supports all kinds of images including PNG.
·         There are five steps for the graphics image to be used in Php
1)     Create Canvas
2)     Allocate background color for the canvas
3)     Allocate foreground color for shapes.
4)     Draw shapes on the canvas.
5)     Place the canvas on the web page.

Note: GD library is already configured in the wamp we use but to start the service click on the icon of wamp in the taskbar. Then click on Php settings and then go to Php extensions and select the option php_gd2.

Ø       GD Library Functions

1)    imagecreate()

This is the first step to draw an image. This function is used for creating the canvas. To create the canvas width and height is to be given. imagecreate () returns an image identifier representing a blank image of the specified size.

Syntax:

(resource) canvas_handler imagecreate (int width, int height)

Example:

<?php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          imagejpeg ($canvas);
?>

2)    imagecolorallocate()

This is the next step for drawing the image on the canvas. This function allocates color to an image. The first call to the imagecolorallocate () fills the background color of the canvas that is created using imagecreate (). The colors which are to be used are specified in RGB format.

Syntax:

(resource) color_handler imagecolorallocate (canvas handler, int red, int green, int blue)

Example: 

 <?php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          imagejpeg ($canvas);
?>

3)    imageline ()

This function is used to draw a line on the canvas. It has six parameters.

Syntax:

bool imageline (canvas handler, int x1, int y1, int x2, int y2, color variable)

Example:

<?php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          $forecolor = imagecolorallocate ($canvas, 255, 255, 255);
          imageline ($canvas, 10, 10, 50, 10, $forecolor);
          imagejpeg ($canvas);
?>

4)    imagerectangle() / imagefilledrectangle ()

·         imagerectangle () function is used to draw a simple rectangle on the canvas.
·         imagefilledrectangle () function is used to draw a rectangle with the color filled in it. Both the functions take six parameters.

Syntax:

bool imagerectangle (canvas handler, int top x, int top y, int bottom x, int bottom y, color variable)

Note: In imagerectangle only the border of the rectangle is drawn with the color specified.
                             Or

bool imagefilledrectangle (canvas handler, int top x, int top y, int bottom x, int bottom y, color variable)

Note: In imagefilledrectangle the rectangle is filled with the color specified.

Example:

                   <?php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          $forecolor = imagecolorallocate ($canvas, 255, 255, 255);
                             imagerectangle ($canvas, 10, 110, 50, 10, $forecolor);
                             imagefilledrectangle ($canvas, 60, 10, 100, 50, $forecolor);
                             imagejpeg ($canvas);
                   ?>

5)    imageellipse() / imagefilledellipse ()

·         imageellipse () function is used to draw a simple ellipse.
·         imagefilledellipse () function is used to draw an ellipse with the color filled in it. Both the functions take six arguments.

Syntax:

bool imageellipse (canvas handler, int center x, int center y, int width, int height, color variable)

                             Or

Bool imagefilledellipse (canvas handler, int center x, int center y, int width, int height, color variable)

Example:

<?Php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          $forecolor = imagecolorallocate ($canvas, 255, 255, 255);
          imageellipse ($canvas, 85, 85, 50, 50, $forecolor);
          imagefilledellipse ($canvas, 25, 85, 50, 50, $forecolor);
          imagejpeg ($canvas);
?>

6)    imagepolygon() / imagefilledpolygon()

·     Polygon means a shape having starting and ending points as the same. A polygon may have many sides as a user wants.
·         imagepolygon () function is used to draw a simple polygon.
·         imagefilledpolygon () function is used to draw a polygon with the color filled in it. Both the functions take 4 parameters.

Syntax:

bool imagepolygon (canvas variable, array variable, int total sides, color    variable);

                             Or

bool imagefilledpolygon (canvas variable, array variable, int total sides, color variable);

Example:

<?php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          $forecolor = imagecolorallocate ($canvas, 255, 255, 255);
          $arr1 = array (10, 175, 25, 150, 50, 180, 10, 175);
          $arr2 = array (60, 175, 85, 150, 110, 180, 60, 175);
          imagepolygon ($canvas, $arr1, 3, $forecolor);
          imagefilledpolygon ($canvas, $arr2, 3, $forecolor);
          imagejpeg ($canvas);
?>

7)    imagearc()

·         imagearc() function is used to draw an arc. It takes 8 arguments.

Syntax:

bool imagearc (canvas variable, int center x, int center y, int width, int height, int starting angle, int ending angle, color variable);

Example:

<?php
          header (“Content-type: image/jpeg”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          $forecolor = imagecolorallocate ($canvas, 255, 255, 255);
          imagearc ($canvas, 50, 250, 50, 50, 0, 180, $forecolor);
          imagejpeg ($canvas);
?>

8)    imagepng() / imagejpeg() / imagegif()

·         This is the last step to place the canvas on the webpage. The image can be in any format. That is either PNG or JPEG or GIF.
·     If PNG image is created and is to be placed on the webpage then imagepng () function is used.
·    If JPEG image is created and is to be placed on the webpage then imagejpeg() function is used.
·         If GIF image is created and is to be placed on the webpage then imagegif() function is used.

Syntax:

 imagepng (canvas variable)

                   Or

           imagejpeg (canvas variable)

                   Or

           imagegif (canvas variable)

Example:

<?php
          header (“Content-type: image/png”);
          $canvas = imagecreate (500,500);
          $bgcolor = imagecolorallocate ($canvas, 255, 0, 0);
          imagejpeg ($canvas);
?>

9)    imagesX()

·         imagesX () function returns the width of the image.

Note: The value returned by imagesX () can be printed with the echo statement on the webpage. But when echo statement is used the image can not be placed on the webpage i.e. imagepng or imagejpeg or imagegif functions will not work.

Syntax:

 int imagesX (canvas variable);

Example

<?php
          $canvas = imagecreate (100, 100);
          echo imagesX ($canvas);
?>

10)            imagesY()

·         imagesY () function returns the height of the image.

Note: The value returned by imagesY () can be printed with the echo statement on the webpage. But when echo statement is used the image can not be placed on the webpage i.e. imagepng or imagejpeg or imagegif functions will not work.

                        Syntax:

int imagesY (canvas variable);

                        Example:

                   <?php
                             $canvas = imagecreate (100, 200);
                             echo imagesY ($canvas);
                   ?>

11)            imagecreatefromjpeg() / imagecreatefrompng()

·     These functions create a new image from a file. These functions are used when working with readymade images. When any readymade image from the file is to be placed on the webpage imagecreatefromjpeg () or imagecreatefrompng () is used.

Syntax:

 resource imagecreatefromjpeg (filename)

                   Or

            resource imagecreatefrompng (filename)

Example:

<?php
          header (“Content-type :image/jpeg”);
          $canvas = imagecreatefromjpeg (“Sunset.jpeg”);
          imagejpeg ($canvas);
?>

12)            imagecopy()

·        imagecopy () is used to copy the image from the source file to the destination file. It has 8 parameters.

Syntax:

bool imagecopy (destination, source, int destination x, int destination y, int source x, int source y, int source width, int source height)

Example:

<?php
          header (“Content-type :image/jpeg”);
          $source = imagecreatefromjpeg (“Sunset.jpeg”);
          $destination = imagecreatefromjpeg (“Winter.jpeg”);
          imagecopy ($destination, $source, 0, 0, 0, 0, 100, 100);
          imagejpeg ($destination);
?>

13)            imagecopymerge()

·         Imagecopymerge () copies and merge the part of an image. Imagecopymerge () is similar to imagecopy () but this function takes an extra parameter that is the percentage.
·         This percentage indicates the part of the source image that should be merged with the destination image.
·         If the percentage used is 100% then this function behaves exactly like imagecopy ().

Syntax:

bool imagecopymerge (destination, source, int destination x, int  destination y, int source x, int source y, int source width, int source height, int percentage)

Example:

<?php
          header (“Content-type :image/jpeg”);
          $source = imagecreatefromjpeg (“Sunset.jpeg”);
          $destination = imagecreatefromjpeg (“Winter.jpeg”);
          imagecopy ($destination, $source, 0, 0, 0, 0, 300, 300, 60);
          imagejpeg ($destination);
?>

14)            imagecreatetruecolor()

·         imagecreatetruecolor () is similar to imagecreate () but the difference is that the imagecreatetruecolor () will create canvas with the true color range. It displays the canvas with black background color.

Syntax:

 resource imagecreatetruecolor (int width, int height);

Example:

<?php
          header (“Content-type :image/jpeg”);
          $canvas = imagecreatetruecolor (500, 500);
          $bgcolor = imagecolorallocate($canvas, 255, 0 ,0);
          Imagejpeg ($canvas);
?>

Note: Even though the (red) color is specified the canvas will be drawn in black color.

Posted By : Hemangi Zala

2 comments: