Professional Documents
Culture Documents
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.
• The file types that can be created and/or edited using the GD library are GIF, JPEG and PNG.
The $start and $end parameters specify the starting and ending angle of the arc in degrees.
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.
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
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
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
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
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);
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
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);
?>
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)
imagepng($new_image,"resized_image.png");
imagedestroy($new_image);
?>
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
// 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);
header("Content-Type: image/png");
imagepng($image);
?>
Modify an image
<?php
// new color
$blue = imagecolorallocate($im, 0, 0, 255); // blue
$red = imagecolorallocate($im, 255, 0, 0); // red
imagepng($im,"modified_image.png");
imagedestroy($im);
?>
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 );
<?php
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
<?php
header("Content-type: image/png");
$img_width = 800;
$img_height = 600;
imagefill($img, 0, 0, $white);
imagepng($img);
?>