Diploma in Web Engineering
Module IX: Using Extensions and Image Manipulation
Rasan SamarasingheESOFT Computer Studies (pvt) Ltd.No 68/1, Main Street, Pallegama, Embilipitiya.
Contents
1. Image Manipulation with PHP2. GD Library3. ImageCreate()4. ImageColorAllocate()5. Drawing shapes and lines6. imageellipse()7. imagearc()8. imagepolygon()9. imagerectangle() 10. imageline() 11. Creating a new image12. Using a Color Fill13. imagefilledellipse()
14. imagefilledarc() 15. imagefilledpolygon() 16. imagefilledrectangle()17. Basic Pie Chart18. 3D Pie Chart19. Modifying Existing Images20. imagecreatefrompng()21. imagecolortransparent()22. imagecopymerge()23. Creating a new image…24. Stacking images…25. Imagestring() 26. Draw a string
Image Manipulation with PHP
• PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files.
• You will need to compile PHP with the GD library for this to work.
• GD and PHP may also require other libraries, depending on which image formats you want to work with.
GD Library
• The GD Graphics Library is a graphics software library by Thomas Boutell for dynamically manipulating images.
• GD stands for "Graphics Draw".
• GD can create images composed of lines, arcs, text, other images, and multiple colors.
Drawing a new image - ImageCreate()
Returns an image identifier representing a blank image of specified size.
resource imagecreate (int $width, int $height)
Define a color - ImageColorAllocate()
Returns a color identifier representing the color composed of the given RGB components.
int imagecolorallocate (resource $image ,int $red ,int $green ,int $blue)
Creating Image
$myImage = imagecreate(250, 250);$black = imagecolorallocate($myImage, 0, 0, 0);header("Content-type: image/png"); // Send a Raw HTTP Headerimagepng($myImage); // Outputs a PNG imageimagedestroy($myImage); // Destroy an image
Drawing shapes and lines
• imageellipse() - Draw an ellipse• imagearc() - Draws an arc• imagepolygon() - Draws a polygon• imagerectangle() - Draw a rectangle• imageline() - Draw a line
imageellipse() - Draw an ellipse
Draws an ellipse centered at the specified coordinates.
bool imageellipse (resource $image, int $cx, int $cy, int $width, int $height, int $color)
imagearc() - Draws an arc
Draws an arc of circle centered at the given coordinates.
bool imagearc (resource $image, int $cx, int $cy, int $width, int $height, int $start, int $end, int $color)
imagepolygon() - Draws a polygon
Creates a polygon in the given image.
bool imagepolygon (resource $image, array $points, int $num_points, int $color)
imagerectangle() - Draw a rectangle
Creates a rectangle starting at the specified coordinates.
bool imagerectangle (resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
imageline() - Draw a line
Draws a line between the two given points.
bool imageline (resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
Creating a new image$myImage = imagecreate(250, 250);
$black = imagecolorallocate($myImage, 0, 0, 0);$white = imagecolorallocate($myImage, 255, 255, 255);$red = imagecolorallocate($myImage, 255, 0, 0);$green = imagecolorallocate($myImage, 0, 255, 0);$blue = imagecolorallocate($myImage, 0, 0, 255);
imagerectangle($myImage, 15, 15, 40, 55, $red);imagerectangle($myImage, 40, 55, 165, 195, $white);
header("Content-type: image/png"); // Send a Raw HTTP Headerimagepng($myImage); // Outputs a PNG image
imagedestroy($myImage); // Destroy an image
Using a Color Fill
• imagefilledellipse() - Draw a filled ellipse• imagefilledarc() - Draw a partial arc and fill it• imagefilledpolygon() - Draw a filled polygon• imagefilledrectangle() - Draw a filled rectangle
imagefilledellipse() - Draw a filled ellipse
Draws an ellipse centered at the specified coordinate on the given image.
bool imagefilledellipse (resource $image, int $cx, int $cy, int $width, int $height, int $color)
imagefilledarc() - Draw a partial arc and fill it
Draws a partial arc centered at the specified coordinate in the given image.
bool imagefilledarc (resource $image, int $cx, int $cy, int $width, int $height, int $start, int $end, int $color, int $style)
imagefilledpolygon() - Draw a filled polygon
Creates a filled polygon in the given image.
bool imagefilledpolygon (resource $image, array $points, int $num_points, int $color)
imagefilledrectangle() - Draw a filled rectangle
Creates a rectangle filled with color in the given image starting at point 1 and ending at point 2. 0, 0 is the top left corner of the image.
bool imagefilledrectangle (resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
Using a Color Fill$myImage = imagecreate(150, 150);
$black = imagecolorallocate($myImage, 0, 0, 0);$white = imagecolorallocate($myImage, 255, 255, 255);$red = imagecolorallocate($myImage, 255, 0, 0);$green = imagecolorallocate($myImage, 0, 255, 0);$blue = imagecolorallocate($myImage, 0, 0, 255);
imagefilledrectangle($myImage, 15, 15, 40, 55, $red);imagefilledrectangle($myImage, 40, 55, 65, 95, $white);
header("content-type: image/png");imagepng($myImage);
imagedestroy($myImage);
Basic Pie Chart$myImage = imagecreate(150, 150);
$white = imagecolorallocate($myImage, 255, 255, 255);$red = imagecolorallocate($myImage, 255, 0, 0);$green = imagecolorallocate($myImage, 0, 255, 0);$blue = imagecolorallocate($myImage, 0, 0, 255);
imagefilledarc($myImage, 50, 50, 100, 100, 0, 90, $red, IMG_ARC_PIE);imagefilledarc($myImage, 50, 50, 100, 100, 91, 180, $green, IMG_ARC_PIE);imagefilledarc($myImage, 50, 50, 100, 100, 181, 360, $blue, IMG_ARC_PIE);
header("Content-type: image/png");imagepng($myImage);
imagedestroy($myImage);
3D Pie Chart$myImage = imagecreate(150, 150);
$white = imagecolorallocate($myImage, 255, 255, 255);$red = imagecolorallocate($myImage, 255, 0, 0);$green = imagecolorallocate($myImage, 0, 255, 0);$blue = imagecolorallocate($myImage, 0, 0, 255);$lt_red = imagecolorallocate($myImage, 255, 150, 150);$lt_green = imagecolorallocate($myImage, 150, 255, 150);$lt_blue = imagecolorallocate($myImage, 150, 150, 255);
for($i = 60; $i > 50; $i--){imagefilledarc($myImage, 50, $i, 100, 50, 0, 90, $lt_red, IMG_ARC_PIE);imagefilledarc($myImage, 50, $i, 100, 50, 91, 180, $lt_green, IMG_ARC_PIE);imagefilledarc($myImage, 50, $i, 100, 50, 181, 360, $lt_blue, IMG_ARC_PIE);}
imagefilledarc($myImage, 50, $i, 100, 50, 0, 90, $red, IMG_ARC_PIE);imagefilledarc($myImage, 50, $i, 100, 50, 91, 180, $green, IMG_ARC_PIE);imagefilledarc($myImage, 50, $i, 100, 50, 181, 360, $blue, IMG_ARC_PIE);
header("Content-type: image/png");imagepng($myImage);imagedestroy($myImage);
Modifying Existing Images
• imagecreatefrompng() - Create a new image from file or URL• imagecolortransparent() - Define a color as
transparent• imagecopymerge() - Copy and merge part of an
image
imagecreatefrompng()
Returns an image identifier representing the image obtained from the given filename.
resource imagecreatefrompng (string $filename)
imagecolortransparent()
Sets the transparent color in the given image.
int imagecolortransparent (resource $image [, int $color ])
imagecopymerge()
Copy a part of src_im onto dst_im starting at the x,y coordinates src_x, src_y with a width of src_w and a height of src_h. The portion defined will be copied onto the x,y coordinates, dst_x and dst_y.
bool imagecopymerge (resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h, int $pct)
Creating a new image from existing image
$myImage = imagecreatefrompng("img1.png");
$white = imagecolorallocate($myImage, 255, 255, 255);
imagefilledellipse($myImage, 55, 170, 50, 20, $white);imagefilledellipse($myImage, 150, 70, 20, 20, $white);imagefilledellipse($myImage, 125, 70, 20, 20, $white);
header("Content-type: image/png");imagepng($myImage);
imagedestroy($myImage);
Stacking images and making them transparent
$baseimage = imagecreatefrompng("img1.png");
for($i=2; $i<5; $i++){$myImage = imagecreatefrompng("img".$i.".png");$gray = imagecolorallocate($myImage, 5, 5, 5);imagecolortransparent($myImage, $gray);imagecopymerge($baseimage, $myImage, 10, 10, 0, 0, 250, 250, 60);}
header("Content-type: image/png");imagepng($baseimage);
imagedestroy($baseimage);
Imagestring() - Draw a string horizontally
Draws a string at the given coordinates.
bool imagestring (resource $image, int $font, int $x, int $y, string $string, int $color)
Draw a string
$myImage = imagecreate(400, 300);$background = imagecolorallocate($myImage, 200, 10, 10);$text = imagecolorallocate($myImage, 20, 200, 20);imagestring($myImage, 5, 10, 20, "Hello World", $text);
header("Content-type: image/png");imagepng($myImage);
imagedestroy($myImage);
The End
http://twitter.com/rasansmn