PHP
  Home arrow PHP arrow Page 4 - 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 - Retrieving information about a given image stream
    ( Page 4 of 4 )

    In consonance with the concepts that I discussed in the section you just read, this last section will be focused upon covering the functions integrated with the GD package that retrieve the physical dimensions of a given image stream.

    To perform these tasks, the library offers the handy "imagex()" and "imagey()" functions, which can be used to retrieve the width and height of a specified image respectively.

    A pair of simple implementations for these functions is shown below, so study the corresponding code samples:

    // example of 'imagesx()' function

    try{
       if(!$image=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating image');
       }
       // display image width
       echo 'The image is '.imagesx($image).'pixels width';
       // displays: The image is 300pixels width
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    // example of 'imagesy()' function try{
       if(!$image=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating image');
       }
       // display image height
       echo 'The image is '.imagesy($image).'pixels height';
       // displays: The image is 200pixels height
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    Admittedly, getting the width and height values of a given image stream is actually a straightforward procedure, considering the simplicity of the functions. As you saw, they only take a reference of the image stream used to calculate its respective dimensions. Quite easy, right?

    Finally, as I said earlier, the GD library also offers another powerful function that fetches relevant information on a concrete image stream. This function is called "getimagesize()." It not only retrieves the respective width and height of the stream, but returns its MIME type as well in an array notation.

    Below I coded a demonstrative example that shows how to use this function: 

    // example of 'getimagesize()' function

       if(!$imageData=getimagesize('clouds.gif')){
         throw new Exception('Error getting image data');
       }
       // display image data
       echo 'Image data is as following<br />';
       echo 'Image width: '.$imageData[0].'px <br />';// Image
    width :300px 
       echo 'Image height: '.$imageData[1].'px <br />';// Image
    height :200px
       echo 'MIME type: '.$imageData['mime'].'<br />';// MIME type:
    image/gif

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

    See how easy it is to retrieve relevant information about a given image stream using the previous "getimagesize()" function? I'm sure you do! Of course, it's worthwhile to mention here that all of the examples included in this tutorial are just that: a friendly introduction to using the most important functions bundled with the GD extension. For full information about this package, the best place to go is the PHP official site.

    Final thoughts

    In this fourth part of the series, I walked you through using the GD functions that draw basic filled shapes, ranging from simple rectangles to more complex polygons. I also showed you a few additional functions for retrieving the dimensions of a specified image stream.

    In the last part of the series, I'll teach you how to take advantage of the capacity offered by the GD package to apply different types of filters to an existing graphic.

    Now that you've been warned, I think you won't want to miss it!  



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek