SunQuest
 
       PHP
  Home arrow PHP arrow Page 3 - 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 
 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 - Drawing filled polygons with the GD library


    (Page 3 of 4 )

    As I explained in the previous section, the GD library comes bundled with functions that display different types of polygons on the browser. I'm going to show you a couple of new functions for drawing filled rectangles, along with polygons that have more than four sides.

    That being said, please take a look at the following code sample. It demonstrates how to use the "imagefilledrectangle" GD function. This function is tasked with displaying different flood filled rectangles on the browser. Here is how the example in question looks:

    // example of 'imagefilledrectangle()' function - draws a white
    rectangle

    try{
       if(!$image=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating image');
       }
       // allocate some basic colors
       if(!$whiteColor=imagecolorallocate($img,255,255,255)){
         throw new Exception('Error allocating color');
       }
       // draw a white rectangle
       imagefilledrectangle($image,10,10,100,100,$whiteColor);
       // display image to the browser
       header("Content-type: image/gif");
       imagegif($image);
       // free memory
       imagedestroy($image);

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

    As shown above, the handy "imagefilledectangle()" function can be useful for displaying quickly a rectangle which has been previously filled with a given foreground color. In this case, the example displays a basic white rectangle positioned at the X,Y coordinates specified by the respective input parameters. This generates an image similar to the one shown below:

    All right, now that you hopefully learned how the previous "imagefilledrectangle()" function works, please pay close attention to the following example. It shows a simple implementation for another handy drawing function, called "imagefilledpolygon()". As you might have guessed, this one draws a predefined polygon, filled with a given foreground color. The way that this operation is carried out is demonstrated by the script below:

    // example of 'imagefilledpolygon()' function

    try{
       if(!$img=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating image');
       }
       // define polygon coordinates
       $coord= array(10,50,50,100,100,30,30,10);
       // allocate blue color
       $blue=imagecolorallocate($img,0,0,255);
       // draw filled polygon
       imagefilledpolygon($img,$coord,4,$blue);
       header("Content-type: image/gif");
       imagegif($img);

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

    Certainly, the "imagefilledpolygon()" function is very convenient for displaying on the browser different types of polygons which have been previously filled with a specified foreground color. The final image created by the previous script looks similar to this one: 

    So far, so good. At this point, I have shown you a considerable number of functions provided by the GD extension that can be used to perform all sorts of flood filling tasks on distinct shapes. This feature makes this library quite helpful for generating more complex graphics from primitive forms.

    Thus, considering that the way all of these functions work is simple to grasp, I'll dedicate the last section of this tutorial to covering a few more handy functions packaged with this graphical library. These functions can be utilized for retrieving specific information about a given image stream, quickly and easily.

    As you'll see in a moment, these functions are really useful, so jump ahead and read the next few lines. I'll be there, waiting for you.

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