Home arrow PHP arrow Page 4 - Drawing Functions and the GD Library in PHP

Using the imageellipse() and imagefilledellipse() functions - PHP

If you're a PHP developer looking for an approachable guide on how to use the popular GD extension available with PHP 4 and PHP 5, then look no further, because this series might be what you need. Welcome to the third article of the series that began with "A Close Look at the GD Library in PHP." In this group of tutorials, you'll find distilled material on using the most important functions that come integrated with the GD library.

TABLE OF CONTENTS:
  1. Drawing Functions and the GD Library in PHP
  2. Taking a look at the imagearc() function
  3. Using the imagechar() and imagecharup() functions
  4. Using the imageellipse() and imagefilledellipse() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
August 21, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: