You are on page 1of 31

Graphics in PHP

GD Library

• The "GD Library" is the most used image-processing tool in PHP, allowing the dynamic
creation and editing of images, charts, thumbnails, etc.

• GD library. GD is an open source code library for the dynamic creation of images. GD is used


for creating PNG, JPEG and GIF images and is commonly used to generate charts, graphics,
thumbnails on the fly

• The file types that can be created and/or edited using the GD library are GIF, JPEG and PNG.

• The GD library will be included with most PHP installations.


• To confirm if the GD library is activated on your server/host, It can run <?php echo
phpinfo(); ?> and scroll down to "GD" to confirm its status.

•The GD library includes over 100 functions:


Draw Circles and Arcs

The function imagearc($image, $cx, $cy, $width, $height, $start, $end,


$color) can draw circular arcs using $cx and $cy as its center.

The $width and $height parameters determine the size of the arc on different axes.

The $start and $end parameters specify the starting and ending angle of the arc in degrees.

To draw complete arcs from 0 to 360 degrees.

It can use the alternative imageellipse($image, $cx, $cy, $width, $height,


$color) function.
Drawing Rectangles, Circle, Arcs and Polygons
Draw Rectangles and Polygons
To draw rectangles over an image using the imagerectangle($image, $x1, $y1, $x2, $y2,
$color) function.

The $x1 and $y1 values determine the top-left corner of the rectangle.

The $x2 and $y2 values determine the bottom-right corner.

There is also an imagepolygon($image, $points, $num_points, $color) function, which can create a


polygon
with any number of sides or points.

The $points parameter is an array where two elements are paired together to get the coordinates of a specific
point. 

Another function called imageopenpolygon() has been added to PHP 7, which does not draw a line between the
first and last point.
Drawing Rectangles, Circle, Arcs and Polygons

Use the imagefilledrectangle() function to draw squares and rectangles, specifying the top left and bottom right corner
positions.

imagefilledrectangle (resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
Use the imagefilledellipse() function to draw circles and ellipses, specifying the center position, width and height of the
shape.

imagefilledellipse (resource $image, int $cx, int $cy, int $width, int $height, int $color)

Use the imagefilledpolygon() function to draw polygons, specifying the three point of the shape.

imagefilledpolygon (resource $image, array $points, int $num_points, int $color)

Each function requires several parameters, first identifying the image to draw on, then identifying the size and/or position
of the object being drawn, and finally specifying the color that the object should be drawn in.
Function Description
imagefilledellipse() Draws a Filled Ellipse
imageellipse() Draws An Ellipse
imagefilledpolygon() Draws a Filled Polygon
imagepolygon() Draws a Polygon
imagefilledrectangle() Draws a Filled Rectangle
imagerectangle() Draws a Rectangle
Create a 200x200 square
<?php
create_image();
print "<img src=image.png?".date("U").">";

function create_image()
{
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow
imagepng($im,"image.png");
imagedestroy($im);
}
?>
<?php
header('Content-type: image/png');
$png_image = imagecreate(300, 300);
$grey = imagecolorallocate($png_image, 199, 199, 199);
$green = imagecolorallocate($png_image, 128, 204, 204);
imagefilltoborder($png_image, 0, 0, $grey, $grey);
imagepng($png_image);
imagedestroy($png_image);
?>
Draw Lines
<?php
create_image();
print "<img src=image.png?".date("U").">";

function create_image(){
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow

$red = imagecolorallocate($im, 255, 0, 0); // red


$blue = imagecolorallocate($im, 0, 0, 255); // blue
imageline ($im, 5, 5, 195, 5, $red);
imageline ($im, 5, 5, 195, 195, $blue);

imagepng($im,"image.png");
imagedestroy($im);
}
?>
Draw Rectangles
<?php
create_image();
print "<img src=image.png?".date("U").">";

function create_image(){
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow

$red = imagecolorallocate($im, 255, 0, 0); // red


$blue = imagecolorallocate($im, 0, 0, 255); // blue

imagerectangle ($im, 5, 10, 195, 50, $red);


imagefilledrectangle ($im, 5, 100, 195, 140, $blue);

imagepng($im,"image.png");
imagedestroy($im);
}
?>
Draw Ellipses
<?php
create_image();
print "<img src=image.png?".date("U").">";

function create_image(){
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image
stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow

$red = imagecolorallocate($im, 255, 0, 0); // red


$blue = imagecolorallocate($im, 0, 0, 255); // blue
imageellipse($im, 50, 50, 40, 60, $red);
imagefilledellipse($im, 150, 150, 60, 40, $blue);

imagepng($im,"image.png");
imagedestroy($im);
}
<?php Draw Aarc
create_image();
print "<img src=image.png?".date("U").">";

function create_image(){
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow

$red = imagecolorallocate($im, 255, 0, 0); // red


$blue = imagecolorallocate($im, 0, 0, 255); // blue

imagearc($im, 20, 50, 40, 60, 0, 90, $red);


imagearc($im, 70, 50, 40, 60, 0, 180, $red);
imagearc($im, 120, 50, 40, 60, 0, 270, $red);
imagearc($im, 170, 50, 40, 60, 0, 360, $red);

imagefilledarc($im, 20, 150, 40, 60, 0, 90, $blue, IMG_ARC_PIE);


imagefilledarc($im, 70, 150, 40, 60, 0, 180, $blue, IMG_ARC_PIE);
imagefilledarc($im, 120, 150, 40, 60, 0, 270, $blue, IMG_ARC_PIE);
imagefilledarc($im, 170, 150, 40, 60, 0, 360, $blue, IMG_ARC_PIE);

imagepng($im,"image.png");
imagedestroy($im);
} ?>
<?php
header('Content-type: image/png');
$png_image = imagecreate(300, 300);
$grey = imagecolorallocate($png_image, 229, 229, 229);
$vi = imagecolorallocate($png_image, 128, 320, 150);
imagefilltoborder($png_image, 0, 0, $grey, $grey);

imagefilledrectangle ($png_image, 20, 20, 80, 80, $vi); // SQUARE


imagefilledrectangle ($png_image, 100, 20, 280, 80, $vi); // RECTANGLE
imagefilledellipse ($png_image, 50, 150, 75, 75, $vi); // CIRCLE
imagefilledellipse ($png_image, 200, 150, 150, 75, $vi); // ELLIPSE

$poly_points = array(150, 200, 100, 280, 200, 280);


imagefilledpolygon ($png_image, $poly_points, 3, $vi); // POLYGON

imagepng($png_image);
imagedestroy($png_image);
?>
<?php
Add Text to the Image
create_image();
print "<img src=image.png?".date("U").">";

function create_image(){
$im = @imagecreate(200, 200)or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow
$red = imagecolorallocate($im, 255, 0, 0); // red

imagestring($im, 1,file:///C:/apache2triad/htdocs/0image/tutorial1.html 5, 10, "Hello !", $red);


imagestring($im, 2, 5, 50, "Hello !", $red);
imagestring($im, 3, 5, 90, "Hello !", $red);
imagestring($im, 4, 5, 130, "Hello !", $red);
imagestring($im, 5, 5, 170, "Hello !", $red);

imagestringup($im, 5, 140, 150, "Hello !", $red);

imagepng($im,"image.png");
imagedestroy($im);
}
?>
Rotate image
<?php

$im = imagecreatefrompng("image.png");
$yellow = imagecolorallocate($im, 255, 255, 0);
$rotate = imagerotate($im, 90,$yellow);
imagepng($rotate,"image_rotated.png");
imagedestroy($im);

print "<img src=image.png> - <img src=image_rotated.png>";

?>
Resize image
<?php

$original_image = imagecreatefrompng("image.png");
// obtain data from selected image
$image_info = getimagesize("image.png");
// data contained in array $image_info may be displayed in next line
// print_r($image_info)

$width = $image_info[0]; // width of the image


$height = $image_info[1]; // height of the image

// we will reduce image size to 70%, so new dimensions must be calculate


$new_width = round ($width*0.7);
$new_height = round ($height*0.7);

$new_image = imagecreate($new_width, $new_height);


imagecopyresized($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagepng($new_image,"resized_image.png");
imagedestroy($new_image);

print "<img src=image.png> <br>Resized image<BR> <img src=resized_image.png>";

?>
Resize image
<?php
$original_image = imagecreatefrompng("image.png");
$image_info = getimagesize("image.png");
$width = $image_info[0]; // width of the image
$height = $image_info[1]; // height of the image
$new_width = round ($width*0.7);
$new_height = round ($height*0.7);
$new_image = imagecreate($new_width, $new_height);
imagecopyresized($new_image, $original_image, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);
imagepng($new_image,"resized_image.png");
imagedestroy($new_image);
print "<img src=image.png> <br>Resized image<BR> <img
src=resized_image.png>";
?>
Get a portion of the image
<?php

// get the original image


$original_image = imagecreatefrompng("image.png");

// create new image


$new_image = imagecreate(200, 200);

// define red color (it will be the background of the new image)
$red = imagecolorallocate($new_image, 255, 0, 0);
imagecopyresized($new_image, $original_image, 75, 75, 0, 0, 100, 100, 100, 100);
imagepng($new_image,"new_image.png");
imagedestroy($new_image);
print "<img src=image.png> <br>New image<BR> <img src=new_image.png>";
?>
Black box within white box

<?php
$image = imagecreate(200, 200);

$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);


$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
imagefilledrectangle($image, 50, 50, 150, 150, $black);

header("Content-Type: image/png");
imagepng($image);
?>
Modify an image
<?php

// get the original image


$im = imagecreatefrompng("image.png");

// new color
$blue = imagecolorallocate($im, 0, 0, 255); // blue
$red = imagecolorallocate($im, 255, 0, 0); // red

imagefilledrectangle ($im, 80, 5, 195, 60, $blue);


imagestring($im, 5, 80, 5, "Modified!", $red);

imagepng($im,"modified_image.png");
imagedestroy($im);

print "<img src=image.png> <br>Modified image<BR> <img


src=modified_image.png>";

?>
Text in images
<?php
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "This is Dr Santosh", $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 180, 45, $line_colour );

header( "Content-type: image/png" );


imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>
Create and show text in images

<?php

header("Content-type: image/png");

$im = @imagecreate(200, 200) or die("Cannot Initialize new GD


image stream");

$background_color = imagecolorallocate($im, 255, 255, 0); //


yellow
$blue = imagecolorallocate($im, 0, 0, 255); // blue

imagestring($im, 3, 5, 5, "My Text String", $blue);

imagepng($im);
imagedestroy($im);

?>
<?php
header("Content-type: image/png");
$img_width = 800;
$img_height = 600;

$img = imagecreatetruecolor($img_width, $img_height);

$black = imagecolorallocate($img, 0, 0, 0);


$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$orange = imagecolorallocate($img, 255, 200, 0);

imagefill($img, 0, 0, $white);

imagerectangle($img, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*8/10, $red);


imagerectangle($img, $img_width*4/10, $img_height*5/10, $img_width*8/10, $img_height*8/10, $red);

imagepolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*5/10], 3, $red);


imageopenpolygon($img, [$img_width*3/10, $img_height*2/10, $img_width*7/10, $img_height*2/10, $img_width*8/10, $img_height*5/10], 3, $red);

imageellipse($img, 100, 100, 100, 100, $orange);


imagearc($img, $img_width*3/10, $img_height*8/10, 100, 200, 180, 360, $red);

imageline($img, 0, $img_height*8/10, $img_width, $img_height*8/10, $green);

imagepng($img);
?>

You might also like