Download presentation
Presentation is loading. Please wait.
1
Higher-level PHP constructs for manipulating image files (contd.)
2
A library of tools for manipulating images So far, we have seen the following GD functions –imageCreateTrueColor create a new true colour image –imageGIF export image in GIF format –imagecolorallocate add a new colour to the palette for an image –imagefilledrectangle add a filled rectangle to an image –imagefill flood fill part of an image –imagefilledellipse add a filled circle or ellipse to an image –imagefilledpolygon add a filled polygon to an image
3
More functions for exporting images We are not restricted to exporting images in the GIF format As well as imageGIF, the GD library provides –imagePNG export image in PNG format Example usage: imagePNG($image,"someFile.png"); –imagejpeg export image in JPG format Example usage: imageJPEG($image,"someFile.jpg");
4
Functions for drawing unfilled shapes We are not restricted to drawing filled shapes As well as imagefilledrectangle, the GD library provides imagerectangle draws an unfilled rectangle Example usage: imagerectangle($image, 50, 50, 150, 150, $someColour); Note that this does not allow us to specify the thickness of the border of the rectangle By default, the thickness is 1 pixel, but we can change this imagesetthickness set the thickness for lines in subsequent drawing in the image Example usage: imagesetthickness($image, 3);
5
Drawing a thick-bordered unfilled rectangle
6
Functions for drawing unfilled shapes (contd.) Similarly, there is imageellipse –Example usage: imageellipse($image, $cx, $cy, $width, $height, $colour); imagepolygon –Example usage: imagepolygon($image, $arrayOfPoints, $numPoints, $colour); imageline –Example usage: imageline($image, $x1, $y1, $x2, $y2, $colour); imagearc –Draws an arc from a circle or ellipse of some width and height –0 degrees is at 3 o’clock and arc is drawn clockwise –Example usage: imageline($image, $cx, $cy, $ellipseWidth,$ellipseHeight, $startAngle,$endAngle, $colour);
7
Drawing a smiley face
8
<?php $im = imageCreateTrueColor(200, 200); $white = imagecolorallocate($im, 255, 255, 255); $red = imagecolorallocate($im, 255, 0, 0); $green = imagecolorallocate($im, 0, 255, 0); $blue = imagecolorallocate($im, 0, 0, 255); $yellow = imagecolorallocate($im, 255, 255, 0); imagefill($im,1,1,$white); // draw filled circle for head imagefilledellipse($im, 100, 100, 200, 200, $yellow); imagesetthickness($im,4); // draw the mouth imagearc($im, 100, 100, 150, 150, 25, 155, $red); // draw left eye and then the right eye imagearc($im, 60, 75, 50, 50, 0, 360, $green); imagearc($im, 140, 75, 50, 50, 0, 360, $blue); imageGIF($im,"head.gif"); ?>
9
Creating our own style of line imagestyle takes two arguments imagestyle($image, $arrayOfPixelColours); To use our newly defined style of line in subsequent drawing statements, we must use the special identified IMG_COLOR_STYLED instead of a colour name
10
Using a thick line of our own style
11
Adding text to an image imagestring takes six arguments imagestring($image, $font, $x, $y, $string, $colour); The font can be 1, 2, 3, 4, 5, where higher numbers produce bigger characters
12
We can also import images and then edit them imagecreatefromgif –Example usages: $im=imagecreatefromgif("someImage.gif"); $im=imagecreatefromgif("someUrlPointingToAGifFile"); Similarly, there is –imagecreatefrompng –imagecreatefromjpeg
13
Drawing on an existing image
14
Making animations with PHP
15
Making an animation with PHP <?php include "GIFEncoder.class.php"; if ($dh = opendir ( "frames/" ) ) { while (false !== ( $dat = readdir ( $dh ) ) ) { if ($dat != "." && $dat != ".." ) {$frames [ ] = "frames/$dat"; $framed [ ] = 5; } } closedir ( $dh ); } $gif = new GIFEncoder($frames,$framed,0,2,0,0,0,"url"); fwrite ( fopen( "myanimation.gif","wb"), $gif- >GetAnimation()); ?>
16
Making an animation with PHP <?php include "GIFEncoder.class.php"; /* Build a frames array from sources... */ if ($dh = opendir ( "frames/" ) ) { while (false !== ( $dat = readdir ( $dh ) ) ) { if ($dat != "." && $dat != ".." ) {$frames [ ] = "frames/$dat"; $framed [ ] = 5; } } closedir ( $dh ); } /* GIFEncoder constructor: image_stream = new GIFEncoder (URL or Binary data 'Sources' int 'Delay times' int 'Animation loops' int 'Disposal' int 'Transparent red, green, blue colors' int 'Source type'); */ $gif = new GIFEncoder($frames,$framed,0,2,0,0,0,"url"); fwrite ( fopen( "myanimation.gif","wb"), $gif->GetAnimation()); ?>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.