DIWE - Using Extensions and Image Manipulation

31
Diploma in Web Engineering Module IX: Using Extensions and Image Manipulation Rasan Samarasinghe ESOFT Computer Studies (pvt) Ltd. No 68/1, Main Street, Pallegama, Embilipitiya.

Transcript of DIWE - Using Extensions and Image Manipulation

Page 1: DIWE - Using Extensions and Image Manipulation

Diploma in Web Engineering

Module IX: Using Extensions and Image Manipulation

Rasan SamarasingheESOFT Computer Studies (pvt) Ltd.No 68/1, Main Street, Pallegama, Embilipitiya.

Page 2: DIWE - Using Extensions and Image Manipulation

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

Page 3: DIWE - Using Extensions and Image Manipulation

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.

Page 4: DIWE - Using Extensions and Image Manipulation

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.

Page 5: DIWE - Using Extensions and Image Manipulation

Drawing a new image - ImageCreate()

Returns an image identifier representing a blank image of specified size.

resource imagecreate (int $width, int $height)

Page 6: DIWE - Using Extensions and Image Manipulation

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)

Page 7: DIWE - Using Extensions and Image Manipulation

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

Page 8: DIWE - Using Extensions and Image Manipulation

Drawing shapes and lines

• imageellipse() - Draw an ellipse• imagearc() - Draws an arc• imagepolygon() - Draws a polygon• imagerectangle() - Draw a rectangle• imageline() - Draw a line

Page 9: DIWE - Using Extensions and Image Manipulation

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)

Page 10: DIWE - Using Extensions and Image Manipulation

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)

Page 11: DIWE - Using Extensions and Image Manipulation

imagepolygon() - Draws a polygon

Creates a polygon in the given image.

bool imagepolygon (resource $image, array $points, int $num_points, int $color)

Page 12: DIWE - Using Extensions and Image Manipulation

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)

Page 13: DIWE - Using Extensions and Image Manipulation

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)

Page 14: DIWE - Using Extensions and Image Manipulation

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

Page 15: DIWE - Using Extensions and Image Manipulation

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

Page 16: DIWE - Using Extensions and Image Manipulation

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)

Page 17: DIWE - Using Extensions and Image Manipulation

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)

Page 18: DIWE - Using Extensions and Image Manipulation

imagefilledpolygon() - Draw a filled polygon

Creates a filled polygon in the given image.

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

Page 19: DIWE - Using Extensions and Image Manipulation

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)

Page 20: DIWE - Using Extensions and Image Manipulation

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

Page 21: DIWE - Using Extensions and Image Manipulation

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

Page 22: DIWE - Using Extensions and Image Manipulation

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

Page 23: DIWE - Using Extensions and Image Manipulation

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

Page 24: DIWE - Using Extensions and Image Manipulation

imagecreatefrompng()

Returns an image identifier representing the image obtained from the given filename.

resource imagecreatefrompng (string $filename)

Page 25: DIWE - Using Extensions and Image Manipulation

imagecolortransparent()

Sets the transparent color in the given image.

int imagecolortransparent (resource $image [, int $color ])

Page 26: DIWE - Using Extensions and Image Manipulation

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)

Page 27: DIWE - Using Extensions and Image Manipulation

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

Page 28: DIWE - Using Extensions and Image Manipulation

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

Page 29: DIWE - Using Extensions and Image Manipulation

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)

Page 30: DIWE - Using Extensions and Image Manipulation

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

Page 31: DIWE - Using Extensions and Image Manipulation

The End

http://twitter.com/rasansmn