Share » Learn » eZ Publish » Image Manipulation with eZ Components

Image Manipulation with eZ Components

Monday 16 January 2006 8:02:00 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

When taking images with a digital camera, a lot of information about the image is stored as part of the image file. This includes for example the date of shooting, the shutter speed, the focal length, etc. This metadata can be quite useful to show to the visitors of your Web site together with the photo. Such information is stored in the EXIF format and it can be read with the help of eZ Components.

The code below shows how to analyze an image and fetch the EXIF information with eZ Components. We use the ezcImageAnalyzer class and create the analysis object stored in the $image PHP object.

Fetching the Image

First we define the image for retrieval and fetch its MIME type to check if it is a JPEG or TIFF, which are the formats currently supporting EXIF.

<?php
// Print image information
$image = new ezcImageAnalyzer( 'boat.jpg' );
 
$mime = $image->mime;
 
// Check if it's a photo format
if ( $mime == 'image/tiff' || $mime == 'image/jpeg' )
{
?>

Date Information

The date is directly available as a UNIX timestamp, providing the created date of the image file, which we format via the PHP date() function.

<?php
$date = date( 'd.m.Y', $image->data->date );
?>

Accessing EXIF Metadata

The actual EXIF information is available directly through the $image->data->exif variable. In this example we fetch the date, shutter speed, aperature and focal length. The shutter speed is being retrieved and its format is converted to fractions of a second, as this is the expected way of displaying this information, and then stored in the $shutterSpeed variable. The aperature is converted to an F stop number and stored in the $aperature variable. We also convert the focal length to a mm value for easier readability. These calculations will be directly available via the eZ Components in the forthcoming version 1.1. At the end of the script, EXIF information is printed out in human readable form.

Sample Script and Result

<?php
 
// Load autoloader functionality
require_once 'Base/base.php';
function __autoload( $className )
{
  ezcBase::autoload( $className );
}
 
// Print image information
$image = new ezcImageAnalyzer( 'boat.jpg' );
 
$mime = $image->mime;
 
// Check if it's a photo format
if ( $mime == 'image/tiff' || $mime == 'image/jpeg' )
{
    $date = date( 'd.m.Y', $image->data->date );
 
    // Calculate shutter speed in seconds
    $shutterSpeedArray = split( '/', $image->data->exif['EXIF']['ExposureTime'] );
    $shutterSpeed = "1/" . $shutterSpeedArray[1] / $shutterSpeedArray[0]  . " sec";
 
    $aperatureArray = split( '/', $image->data->exif['EXIF']['FNumber'] );
    $aperature = "F" . round( $aperatureArray[0] / $aperatureArray[1], 1 );
 
    $focalLengthArray = split( "/", $image->data->exif['EXIF']['FocalLength'] );
    $focalLength = round( $focalLengthArray[0] / $focalLengthArray[1], 2 ) . " mm";
 
    // Print information about the photo
    print( "<dl>
             <di>Photo taken on:</di><dd>$date</dd>
             <di>Shutter speed:</di><dd>$shutterSpeed</dd>
             <di>Aperature:</di><dd>$aperature</dd>
             <di>Focal:</di><dd>$focalLength</dd>
            </dl>" );
}
 
?>

The image used in the script is shown below.

The Output of the script is:

Photo taken on: 24.10.2005 

Shutter speed: 1/160 sec 

Aperature: F5 

Focal: 50 mm
36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)