Section 16.4. More Shapes


16.4. More Shapes

Using three new functions, we can make a much more complicated image. These are: imagecreatetruecolor( ), imagefilledellipse( ), and imagefilledarc( ).

Here is a script using these new functions:

     header("content-type: image/png");     $image = imagecreatetruecolor(400,300);     $blue = imagecolorallocate($image, 0, 0, 255);     $green = imagecolorallocate($image, 0, 255, 0);     $red = imagecolorallocate($image, 255, 0, 0);     imagefilledellipse($image, 200, 150, 200, 200, $red);     imagefilledellipse($image, 200, 150, 180, 180, $blue);     imagefilledellipse($image, 200, 150, 50, 50, $red);     imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green, IMG_ARC_PIE);     imagefilledarc($image, 200, 150, 200, 200, 255, 285, $green, IMG_ARC_PIE);     imagefilledarc($image, 200, 150, 200, 200, 165, 195, $green, IMG_ARC_PIE);     imagefilledarc($image, 200, 150, 200, 200, 75, 105, $green, IMG_ARC_PIE);     imagepng($image);     imagedestroy($image); 

The output from that script is shown in Figure 16-3.

Figure 16-3. Ellipses and circles


Using imagecreatetruecolor( ) is the same as imagecreate( )it takes the same two parameters, and returns an image resource that is freed using imagedestroy( ). The difference between the two is that imagecreatetruecolor( ) returns an image with a true-color palette, whereas an image made by imagecreate( ) cannot contain more than 256 colors. Furthermore, the image resource returned by imagecreatetruecolor( ) automatically has a black background, so you needn't worry about the first allocated color being used as the image background color.

The two new shape functions take several parameters, so you may need to keep the list at hand when working with them. The parameters for imagefilledellipse( ) are: image resource, center of ellipse (X coordinate), center of ellipse (Y coordinate), height, width, and color. As there are more parameters required to draw an arc, imagefilledarc( ) is more complicated again: image resource, center X, center Y, height, width, then the start and end points of the arc specified in degrees, followed by color and, finally, the type of arc to draw.

The start and end points for arcs are specified from 0 to 359 degrees, with 0 pointing directly to the right, or 3 o'clock if you think in clock faces. To draw a complete circle rather than just a section, as in the example, you would specify 0 and 359 as the start and end points; although, in this case, it is easier just to use imagefilledellipse( ). The final parameter to imagefilledarc( ) is the type of arc to draw, and you have the choice of the following:

  • IMG_ARC_PIE, as in the previous example, which draws a filled wedge shape with a curved edge

  • IMG_ARC_CHORD, which draws a straight line between the starting and ending angles

  • IMG_ARC_NOFILL, which draws the outside edge line without drawing the two lines toward the center of the arc

  • IMG_ARC_EDGED, which draws an unfilled wedge shape with a curved edge

You can combine these four together in various ways to make your own style of arc, with the exception of IMG_ARC_CHORD and IMG_ARC_PIE, which cannot be combined together because they conflict geometrically. Some examples:

     imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green,             IMG_ARC_CHORD | IMG_ARC_NOFILL);     imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green,             IMG_ARC_EDGED | IMG_ARC_NOFILL); 

If we use those to replace the first and third calls from the previous script, they should make the righthand arc become a straight line on the outside edge of the arc, and make the lefthand arc become an unfilled wedge. This is pictured in Figure 16-4.

So far, we've only been looking at the filled shapes, but there are unfilled varieties too: imageellipse( ) complements imagefilledellipse( ), imagearc( ) complements imagefilledarc( ), and imagerectangle( ) complements imagefilledrectangle( ). The first and last of these work the same, whether they are filled or otherwise, but imagefilledarc( ) is slightly differentyou don't need the last parameter, because the arc is always the equivalent of IMG_ARC_NOFILL.

Figure 16-4. Now we've tweaked the last parameter to imagefilledarc( ) for the first and third calls




PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net