GD Library
About GD Library

You Know that PHP is basically use for Creating Dynamic WebSite.
Through GD Library You can make Dynamic Image.

For creating Dynamic image this necessary to create new blank image,
you can also create image through GIF, JPEG,PNG etc.

PHP function to create new image.

imagecreate()
This function create new blank image.It takes two argument means x coordinate and y coordinate.
Example:-
imagecreate(200,150);
Where 200 is x coordinate and 150 is y coordinate.

imagecreatetruecolor()
This function create new blank image.It takes two argument means x coordinate and y coordinate.
Example:-
imagecreatetruecolor(200,150);
Where 200 is x coordinate and 150 is y coordinate.

imagecreatefromgif()
This function create new image from GIF file .It takes one string that is name of gif file.
Example:-
imagecreatefromgif('image/image1.gif');
here (image/image1.gif) is path of the image.

imagecreatefromjpeg()
This function create new image from JPEG file. It takes one string that is name of JPEG file.
Example:-
imagecreatefromjpeg('image/image1.jpeg);
here (image/image1.jpeg) is path of the image.

imagecreatefrompng()
This function create new image from PNG file. It takes one string that is name of PNG file.
Example:-
imagecreatefromjpeg('image/image1.png');
here (image/image1.png) is path of the image.

PHP function to work on image

imagecolorallocate()
This functiono fill color in the image that is created by imagecreate function.
int imagecolorallocate(resource image, int red, int  green, int blue);


imageline()
To draw a line on image it needs to have start and end points, so you must do it with an extra set of coordinates as parameters
int imageline(resource image, int x1,int y1,int x2,int y2,int color);

header()
This function to send a header to the web browser telling it the type of data that it is about to receive. This ensures that the browser displays the contents correctly.
Some Example of headers:-
header("Content-type: image/png");
header("Content-type: image/jpeg");

imagepng()
This function send PNG image to the browser.
imagepng($img);
here $img is a resource image.

imagejpeg()
This function send JPEG image to the browser.
imagejpeg($img);
here $img is a resource image.

imagegif()
This function send GIF image to the browser.
imagegif($img);
here $img is a resource image.

imagedestroy()
Finally use this function to remove the image from memory using the imagedestroy() function.

imagerectangle()
This function is use to draw a rectangle on the image.
int imagerectangle (resource image,int x1,int y1,int x2,int y2,int col)

imagellipse()
This function is use to draw circles and ellipses on the image
imageellipse(resource image,int x,int y,int width,int height,int col);

imagestring()
Using this function  you can add text on image.
imagestring(image, font, x, y, text, color);

imagesx()
This function returns the width of the image.
imagesx(resource image);

imagesy()
This function returns the height of the image.
imagesy(resource image);

Example 1:- PHP Script to create image and write text on image
<?php
$im=imagecreate(200, 50);
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Example 2:- PHP Script to create image and draw line on image.
<?php
$im=imagecreate(200, 100);
$background_color = imagecolorallocate($im, 204, 204, 204);
$text_color = imagecolorallocate($im, 0, 0, 0);
imageline($im, 15, 35, 120,60 ,$text_color);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Example 3:- PHP Script to create image from jpeg file and send output to the browser.

<?php
$im=imagecreatefromjpeg('djwebsite/image/dolgachov090200067.jpg');
header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
?>

Example 4:- PHP Script to create image from jpeg file and write text on image.

<?php
$im=imagecreatefromjpeg('djwebsite/image/dolgachov090200067.jpg');
$background_color = imagecolorallocate($im, 204, 204, 204);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im,"arial",20,20,"This is good",$text_color);
header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
?>


Previous
Next