IBM developerWorks
 
       PHP
  Home arrow PHP arrow Page 4 - Drawing Functions and the GD Library i...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Drawing Functions and the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-08-21

    Table of Contents:
  • Drawing Functions and the GD Library in PHP
  • Taking a look at the imagearc() function
  • Using the imagechar() and imagecharup() functions
  • Using the imageellipse() and imagefilledellipse() functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

       · Over the course of this second installment of the series, you'll learn the basis on...
       · From the article, in regards to imagecharup(): "the character has been rotated 90...
       · Thank you commenting on my PHP article and for pointing me that correction on using...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway