GD Lib
GD Lib
GD Lib
Generally, there are 5 steps for the graphics image to be used in PHP.
(1) Create Canvas.
(2) Allocate background color for canvas.
(3) Allocate foreground color for shapes.
(4) Draw shapes on the canvas.
(5) Place the canvas on the web-page.
Canvas Function
imagecreate()
It is the first step to draw the image. This function is used for
creating the canvas. To create the canvas fixed width and height is to
be given.
imagecreatetruecolor()
This function is similar to imagecreate() function, but the difference
is that using the function imagecreatetruecolor() it will create a canvas
with true color range (Black color).
Example:
<?php
$canvas = imagecreate(500,400);
$backcolor = imagecolorallocate($canvas, 50,100,20);
?>
Shape Functions
imageline()
This function is used to draw line on the canvas. It has 6
parameters to be taken as input.
1
Syntax: imageline(canvas_variable, int x1, int y1, int x2, int y2,
color_variable)
Example:
<?php
$canvas = imagecreate(500,400);
$backcolor = imagecolorallocate($canvas, 0, 0, 0);
$shapecolor = imagecolorallocate($canvas, 1, 1, 1);
imageline($canvas, 200, 200, 250, 300, $shapecolor);
?>
imagerectangle() or imagefilledrectangle()
imagerectangle() function is used to draw simple / empty rectangle.
imagefilledrectangle() function is used to draw a rectangle with the
color filled in it.
Both the functions take the same arguments; it takes 6 parameters as
input.
Syntax:
imagerectangle (canvas_variable, int Top X, int Top Y, int Bottom X, int
Bottom Y, color_variable)
OR
imagefilledrectangle (canvas_variable, int Top X, int Top Y, int Bottom X,
int Bottom Y, color_variable)
Example:
<?php
$canvas = imagecreate(500,400);
$backcolor = imagecolorallocate($canvas, 0, 0, 0);
$shapecolor = imagecolorallocate($canvas, 1, 1, 1);
imagerectangle($canvas, 100, 50, 350, 200, $shapecolor);
imagefilledrectangle($canvas, 150, 250, 400, 350, $shapecolor);
?>
imageellipse() or imagefilledellipse()
imageellipse() function is used to draw simple / empty ellipse.
imagefilledellipse() function is used to draw an ellipse with the color
filled in it.
Both the functions take the same arguments; it takes 6 parameters as
input.
Syntax:
imageellipse (canvas_variable, int Center X, int Center Y, int width, int
height, color_variable)
OR
imagefilledellipse (canvas_variable, int Center X, int Center Y, int width,
int height, color_variable)
Example:
<?php
$canvas = imagecreate(500,400);
$backcolor = imagecolorallocate($canvas, 0, 0, 0);
$shapecolor = imagecolorallocate($canvas, 1, 1, 1);
imageellipse($canvas, 200, 100, 50, 50, $shapecolor);
2
imagefilledellipse($canvas, 300, 250, 50, 50, $shapecolor);
?>
imagepolygon() or imagefilledpolygon()
Polygon means a shape having starting and ending point as the same.
A polygon may have many sides as a user wants.
imagepolygon() function is used to draw simple / empty polygon.
imagefilledpolygon() function is used to draw an polygon with the
color filled in it.
Both the functions take the same arguments; it takes 4 parameters as
input.
Syntax:
imagepolygon (canvas_variable, array variable, int total sides,
color_variable)
OR
imagefilledpolygon (canvas_variable, array variable, int total sides,
color_variable)
Example:
<?php
$canvas = imagecreate(400,300);
$backcolor = imagecolorallocate($canvas, 0, 0, 0);
$shapecolor = imagecolorallocate($canvas, 1, 1, 1);
$coords = (100, 250, 200, 150, 300, 250, 100, 250);
imagepolygon($canvas, $coords, 3, $shapecolor);
$coords2 = (100, 150, 200, 50, 300, 150, 100, 150);
imagefilledpolygon($canvas, $coords2, 3, $shapecolor);
?>
imagearc()
This function is used to draw an arc. It has 8 parameters into it.
The angles are 0, 90, 180, 270, 360 and the angle will be seen as below.
270
180 0, 360
90
Example:
<?php
$canvas = imagecreate(400,300);
$backcolor = imagecolorallocate($canvas, 0, 0, 0);
$shapecolor = imagecolorallocate($canvas, 1, 1, 1);
imagearc ($canvas, 200, 150, 15, 5, 0, 180, $shapecolor);
?>
3
Depending on the user, the images are to be created using this
format functions.
Syntax:
imagepng (canvas_variable)
OR
imagegif (canvas_variable)
OR
imagejpeg (canvas_variable)
Example:
<?php
$canvas = imagecreate(400,300);
$backcolor = imagecolorallocate($canvas, 0, 0, 0);
$shapecolor = imagecolorallocate($canvas, 1, 1, 1);
imagearc ($canvas, 200, 150, 15, 5, 0, 180, $shapecolor);
imagepng ($canvas);
?>
It will create an PNG format having an arc of white color drawn on the
canvas with black background.
Miscellaneous Functions
imagesx()
This function will return the width of the image. The return type is
integer.
Example:
<?php
$canvas = imagecreate (400, 300);
echo imagesx($canvas);
?>
It displays 400 which is the width of the canvas.
imagesy()
This function will return the height of the image. The return type is
integer.
Example:
<?php
$canvas = imagecreate (400, 300);
echo imagesy($canvas);
?>
It displays 300 which is the width of the canvas.
imagescreatefromjpeg() or
imagecreatefrompng()
These functions are useful when working with images that are
loaded using a function.
Syntax:
4
imagecreatefromjpeg (filename having extension jpeg)
OR
imagecreatefrompng (filename having extension png)
Example:
<?php
$load = imagecreatefromjpeg (“sunset.jpg”);
imagejpeg($load);
?>
It will load the image sunset.jpg and display it on the web page.
imagecopy()
This function is used to copy the image from source file into
destination file. It has 8 parameters to be given.
Syntax:
imagecopy (destination, source, int destX, int dest Y, int src X, int src Y,
int src width, int src height)
Example:
<?php
$src = imagecreatefromjpeg (“sunset.jpg”);
$des = imagecreatefromjpeg (“winter.jpg”);
imagecopy ($des, $src, 20, 20, 0, 0, 200, 300);
imagejpeg($des);
?>
It will copy the image from sunset.jpg into winter.jpg. The source image
copied on destination has width and height of 200, 300 respectively.
imagecopymerge()
This function is similar to that of the imagecopy() but here the other
parameter that is the percentage from 1 to 100% is to be given. This
percent indicates that the source image should be merged with the
destination image. If the percentage used is 100%, then it behaves
exactly like imagecopy().
Syntax:
imagecopymerge (destination, source, int destX, int dest Y, int src X, int
src Y, int src width, int src height, int percent)
Example:
<?php
$src = imagecreatefromjpeg (“sunset.jpg”);
$des = imagecreatefromjpeg (“winter.jpg”);
imagecopymerge ($des, $src, 20, 20, 0, 0, 200, 300, 50);
imagejpeg($des);
?>
It will copy the sunset.jpg image into winter.jpg image. The percentage
used is 50 so the image copied will be little bit transparent to merge
with destination image.