GD Lib

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Graphical Display (GD Library)

PHP provides a feature to draw any images using various kinds of


shapes. The area where the shapes are to be drawn is known as
Canvas.

There are different versions of Graphical display in PHP.


 Gd-1.6 which supports GIF images and not PNG.
 Ge-2.0.28 supports all kinds of images including PNG.

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.

Syntax: canvas_handler = imagecreate(int width, int height)

Example: $canvas = imagecreate(500,400);


Above statement creates a canvas of width 500 and height 400.

 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).

Syntax: canvas_handler = imagecreatetruecolor (int width, int height)

Example: $canvas = imagecreatetruecolor (500,400);


It will create a canvas of width 500 and height 400 having true color
(black).

Color Allocation Function


 imagecolorallocate()
It is the next step for drawing the image on the canvas. This
function is used for allocating the color to background of canvas and the
shapes which are drawn. The colors which are to be used are in RGB
(Red,Green,Blue) format.

Syntax: color_handler = imagecolorallocate(canvas_handler, int red, int


green, int blue)

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)

If width = height then it will draw a circle


If width > height then it will draw horizontal ellipse
If width < height then it will draw vertical ellipse

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.

Syntax: imagearc (canvas_variable, int Center X, int Center Y, int width,


int height, int starting angle, int ending angle, color_variable)

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);
?>

Place image on web page functions


 imagepng() or imagegif() or imagejpeg()
The last step is to place this canvas on the web page. This image
can be in any format that is either PNG, GIF, or JPEG.

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.

Syntax: int = imagesx(image_handler)

Where image_handler is the variable containing the image information.

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.

Syntax: int = imagesy(image_handler)

Where image_handler is the variable containing the image information.

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.

You might also like