Home arrow PHP arrow Page 4 - Drawing More Complex Shapes with the GD Library in PHP

Retrieving information about a given image stream - PHP

The GD library that comes bundled with PHP 4 and PHP 5 provides web developers with a robust set of intuitive functions for generating and handling dynamic graphics with minor hassles. If you're interested in mastering the numerous features integrated with this PHP extension, this series of instructive tutorials will make the whole learning experience a truly painless process.

TABLE OF CONTENTS:
  1. Drawing More Complex Shapes with the GD Library in PHP
  2. Displaying filled images and arcs
  3. Drawing filled polygons with the GD library
  4. Retrieving information about a given image stream
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
August 22, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: