Drawing Functions and the GD Library in PHP - Using the imageellipse() and imagefilledellipse() functions
(Page 4 of 4 )
In accordance with the concepts that I expressed in the previous section, the GD library also offers a powerful set of functions for drawing basic shapes, such as ellipses, rectangles, and other polygons. However, for now, I'm going to show you only a couple of these functions, which in this case are responsible for displaying some simple ellipses on a given image stream.
That being said, here's an example that shows how to use the brand new "imageellipse()" function to display this shape on the browser. The corresponding code sample is as follows:
// example of 'imageellipse()' function
try{
if(!$img=imagecreatetruecolor(300,200)){
throw new Exception('Error creating image');
}
// fill the background color
$bg=imagecolorallocate($img,0,0,0);
// allocate elipse color
$colEllipse=imagecolorallocate($img,255,255,255);
// draw the ellipse
imageellipse($img,150,100,100,200,$colEllipse);
// display ellipse on the browser
header("Content-type: image/gif");
imagegif($img);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As you can see, the above "imagellipse()" function is indeed very easy to grasp. It takes up as its incoming arguments an image stream resource, and them the corresponding start and end coordinates of the ellipse in question, and finally the color to be used with this shape.
Based upon the signature of the previous code sample, here's the output that it displays on the browser:

In addition, the GD extension offers the "imagefilledellipse()" function, which behaves very similarly to the one that you learned before. In this case, however, it draws a filled ellipse on a specific image stream.
Given that, a basic implementation for this function is shown below:
// example of 'imagefilledellipse()' function
try{
if(!$img=imagecreatetruecolor(300,200)){
throw new Exception('Error creating image');
}
// allocate blue color
$blue=imagecolorallocate($img,0,0,255);
// draw filled ellipse
imagefilledellipse($img,100,90,150,100,$blue);
header("Content-type: image/gif");
imagegif($img);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As you can see, the above "imagefilledellipse()" function is nearly identical to its counterpart "imageellipse()." However, it displays a filled ellipse on a specified image stream, as clearly illustrated by the below image:

All right, at this stage hopefully you have a much better idea of how to draw some basic shapes by using the functions that come bundled with the GD extension. For now, I recommend that you use the source code of all the examples shown here to improve your skills with using this powerful graphical library.
Final thoughts
In this third part of the series, I stepped you through using some of the most useful functions included with the GD library to display some basic string characters, on the browser, as well as a few simple arcs and ellipses.
In the next tutorial, things will get even more interesting, since I'll teach you how to draw many other shapes by using the functionality provided by the GD extension.
Now that you've been warned, you won't want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |