PHP
  Home arrow PHP arrow Page 3 - Drawing More Complex Shapes with 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? 
Google.com  
PHP

Drawing More Complex Shapes with the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    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:
      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 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek