PHP
  Home arrow PHP arrow Page 4 - Drawing Functions and the GD Library in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 10
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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!



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT