16.2. Image Classes

Perforce Chronicle comes with set of image driver classes for each of the supported extensions and a wrapping class that provides image manipulation by using a given image driver.

16.2.1. Image Drivers

Image drivers do the actual work in terms of processing supported image transformations. Each image driver must implement P4Cms_Image_Driver_Interface.

Chronicle ships with the following image drivers, one for each of the supported library:

  • P4Cms_Image_Driver_Imagick for the imagick extension.
  • P4Cms_Image_Driver_Gd for the gd extension.

[Warning] Ensure Desired Extension is Installed

The desired image drive class can only be instantiated if the associated extension is installed.

16.2.2. Factory

An image driver object can be obtained from the P4Cms_Image_Driver_Factory class:

<?php
// get image driver using the gd library
try {
    $driver = P4Cms_Image_Driver_Factory::create('P4Cms_Image_Driver_Gd');
} catch (P4Cms_Image_Exception $e) {
    // gd image driver cannot be instantiated - perhaps because the gd extension is not installed
}

The factory class also provides a way to create a default image driver. It returns the first driver whose class can be instantiated:

<?php
// get some image driver
try {
    $driver = P4Cms_Image_Driver_Factory::create();
} catch (P4Cms_Image_Exception $e) {
    // factory is unable to find any image driver that can be instantiated
}

16.2.3. Image

The P4Cms_Image component provides a general API for manipulating with images. It allows setting and retrieving image data, setting image driver, and performing image transformations (the available transformations are driver dependent). Multiple transformations may be invoked, which are processed in order when image data is requested.

Here is an example of printing an image resized to 200x100 pixels and sharpened via the imagick driver:

<?php
    try{
        // assuming $data contains original image data
        $image = new P4Cms_Image;
        $image->setData($data);

        // set imagick driver
        $driver = P4Cms_Image_Driver_Factory::create('P4Cms_Image_Driver_Imagick');
        $image->setDriver($driver);

        // resize and sharpen image
        $image->transform('resize', array(200, 100))
              ->transform('sharpen');

        // print the image
        header('Content-type: image/jpeg');
        echo $image->getData('jpeg');
    } catch (P4Cms_Image_Exception $e) {
        echo 'Cannot process image.';
    }

Perforce Chronicle - Release: 2012.2/486814