PHP
  Home arrow PHP arrow Page 2 - Drawing More Complex Shapes with the G...
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 
IBM developerWorks
 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 More Complex Shapes with the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-08-22

    Table of Contents:
  • Drawing More Complex Shapes with the GD Library in PHP
  • Displaying filled images and arcs
  • Drawing filled polygons with the GD library
  • Retrieving information about a given image stream

  • 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 More Complex Shapes with the GD Library in PHP - Displaying filled images and arcs


    (Page 2 of 4 )

    As I stated in the beginning of this series, the GD extension is useful for creating dynamic image streams from scratch, and offers a remarkable capacity for preprocessing existing graphics. Now we are going to see how well it fills up different types of basic shapes before displaying them on the browser.

    This is the purpose of the brand new "imagefill()" and "imagefilledarc()" functions. As their names suggest, they perform a filling operation on a given image stream. The function called "imagefill()" performs a flood fill on a specified image; its use is shown in the code sample below: 

    // example of 'imagefill()' function

    try{
       if(!$img=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating image');
       }
       // allocate blue color
       $blue=imagecolorallocate($img,0,0,255);
       imagefill($img,0,0,$blue);
       header("Content-type: image/gif");
       imagegif($img);

    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the "imagefill()" function is really handy for flood filling a specified image stream with a predefined foreground color. It starts this operation at the X, Y coordinates, which are naturally specified as part of the corresponding input parameters.

    So far, so good, right? Now that you hopefully learned how the "imagefill()" GD function works, let me show you another example. In this case, the "imagefilledarc()" is used, not surprisingly, to fill in a basic arc with a specific foreground color.

    That being explained, here's the corresponding code sample that shows a simple implementation for this function:

    // example of 'imagefilledarc()' function try{
       if(!$img=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating image');
       }
       // allocate blue color
       $blue=imagecolorallocate($img,0,0,255);
       // draw filled arc
       imagefilledarc($img,50,50,100,50,0,90,$blue,IMG_ARC_PIE);
       header("Content-type: image/gif");
       imagegif($img);

    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    In this case, the "imagefilledarc()" function that comes bundled with the GD library is used to display a filled arc with a predetermined foreground color on the browser. Also, the referenced function takes the starting and ending coordinates of the arc as its input parameters, along with the image stream used to display the filled shape.

    Given that, the image created by the previous hands-on example is similar to the one shown below:

    Certainly, after you analyze in detail the previous practical example, you'll agree with me that drawing and flood filling basic shapes by using the specific functions provided by the GD extension is truly a no-brainer process that can be performed with minor difficulties. Thus, having covered these two handy functions of this library, it's time to explore others that are also part of the library.

    In the following section I'll teach you how to use a few additional functions that come packaged with the GD extension to display some other filled shapes on the browser, like basic polygons.

    Want to see how this will be achieved? Click on the link that appears below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this part of the series, you'll learn the key concepts on how to use the set of...
     

       

    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 2 hosted by Hostway