Zeta Components - high quality PHP components

eZ Components - ImageAnalysis

Introduction

The ImageAnalysis component allows you to analyze certain image attributes.

Class overview

ezcImageAnalyzer is the main class for this component. It is responsible for handling the analysis of image files, as well as caching the results.

Usage

MIME type determination

The following example simply detects the MIME type of an image and prints it:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. $tutorialPath dirname__FILE__ );
  4. $image = new ezcImageAnalyzer"{$tutorialPath}/img/imageanalysis_example_01.jpg" );
  5. echo "Image has MIME type <{$image->mime}>.\n";
  6. ?>

On line 5, a new ezcImageAnalyzer object is instantiated. This must be done for each image to be analyzed. In line 7, the MIME type is determined. Here is an example image including the output:

Simple JPEG image.Simple JPEG image.
Image has MIME type <image/jpeg>

Extracting further data

Aside from the MIME type, ImageAnalysis can extract other image information. The following example demonstrates this:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. $tutorialPath dirname__FILE__ );
  4. $image = new ezcImageAnalyzer$tutorialPath.'/img/imageanalysis_example_02.jpg' );
  5. echo "Image data:\n";
  6. echo "MIME type:\t{$image->mime}\n";
  7. echo "Width:\t\t{$image->data->width} px\n";
  8. echo "Height:\t\t{$image->data->height} px\n";
  9. echo "Filesize:\t{$image->data->size} b\n";
  10. $comment = ( $image->data->comment == '' ) ? 'n/a' $image->data->comment
  11. echo "Comment:\t{$comment}\n";
  12. ?>

The example is basically the same as the first one, except that more data is requested from ezcImageAnalyzer (lines 8 to 11). The analysis of additional data begins on line 9. After that, the data is cached in the ezcImageAnalyzer object.

The width, height and size values are available for every analyzable image. A comment is not always available. If an image property is not available, the output will be some sensible default value (such as n/a). (Note that the availability of some data also depends on the availability of PHP's Exif extension.)

The example image and printed output is shown below:

Image data: MIME type: image/jpeg Width: 380 px Height: 285 px Filesize: 25984 b Comment: n/a

Configuring handlers

Like ezcImageConverter, ezcImageAnalyzer is based on handler classes, which allow it to utilize different back-ends for image analysis. The currently implemented handlers are:

ezcImageAnalyzerPhpHandler
This uses PHP's getimagesize() function (which does not require the GD extension!) and can optionally use PHP's Exif extension.
ezcImageAnalyzerImagemagickHandler
Here ImageMagick's "identify" program is used.

Both handlers are activated by default and are capable of determining if their preconditions are fulfilled.

You might need to configure a handler, if for example the path to the ImageMagick "identify" binary is not available in the $PATH environment variable. The following example shows how this is possible and what else can be configured for the handlers:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. $tutorialPath dirname__FILE__ );
  4. ezcImageAnalyzer::setHandlerClasses(
  5.     array(
  6.         'ezcImageAnalyzerImagemagickHandler=> array( 'binary' => '/usr/bin/identify' ),
  7.     )
  8. );
  9. $image = new ezcImageAnalyzer$tutorialPath.'/img/imageanalysis_example_03.jpg' );
  10. echo "Image data:\n";
  11. echo "MIME type:\t{$image->mime}\n";
  12. echo "Width:\t\t{$image->data->width} px\n";
  13. echo "Height:\t\t{$image->data->height} px\n";
  14. echo "Filesize:\t{$image->data->size} b\n";
  15. $comment = ( $image->data->comment == '' ) ? 'n/a' $image->data->comment
  16. echo "Comment:\t{$comment}\n";
  17. ?>

Basically, the code is the same as in example 2, except that ezcImageAnalyzer is being configured to only use its ImageMagick handler and not the PHP handler. In addition, the location of the "identify" binary is explicitly set. See the results below:

Image data: MIME type: image/jpeg Width: 320 px Height: 240 px Filesize: 26365 b Comment: San Francisco airport, October 2005.

More information

For more information, see the ezcImageAnalyzer API documentation.