Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Image Class Reference

Perforce image manipulation API. More...

List of all members.

Public Member Functions

 getData ($type=null)
 Get the current data for the image including any pending transformations.
 getDriver ()
 Get the graphics library driver instance.
 getImageSize ()
 Return the resulting image dimensions after pending transforms have been applied.
 hasDriver ()
 Determine if an image driver has been set.
 isSupportedType ($type)
 Check if the given image type is supported.
 setData ($data=null)
 Set the data of the image.
 setDriver (P4Cms_Image_Driver_Interface $driver=null)
 Set the graphics library to use.
 transform ($transform, $args=array())
 Push the given transformation on to the queue.

Protected Member Functions

 _processQueue ()
 Process any pending transformations.

Protected Attributes

 $_driver = null
 $_transforms = null

Detailed Description

Perforce image manipulation API.

The actual work is handled by implementations of P4Cms_Image_Driver_Interface.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

P4Cms_Image::_processQueue ( ) [protected]

Process any pending transformations.

    {
        $queue = $this->_transforms;

        // if there is a queue, apply all transformations in it.
        if ($queue) {
            $driver = $this->getDriver();
            while (!$queue->isEmpty()) {
                $transform = $queue->dequeue();
                call_user_func_array(array($driver, "transform"), $transform);
            }
        }

        $this->_transforms = null;
    }
P4Cms_Image::getData ( type = null)

Get the current data for the image including any pending transformations.

Parameters:
string$typeoptional - the requested image format
Returns:
string the binary image data
    {
        // apply pending transformations
        $this->_processQueue();

        return $this->getDriver()->getData($type);
    }
P4Cms_Image::getDriver ( )

Get the graphics library driver instance.

If there is no existing driver, function will try to create and set a default one.

    {
        // if no driver has been set, try to create default one
        if (!$this->_driver) {
            $this->_driver = P4Cms_Image_Driver_Factory::create();
        }

        return $this->_driver;
    }
P4Cms_Image::getImageSize ( )

Return the resulting image dimensions after pending transforms have been applied.

Returns:
array first element is width in pixels, second element is height in pixels
    {
        // apply pending transformations
        $this->_processQueue();

        return $this->getDriver()->getImageSize();
    }
P4Cms_Image::hasDriver ( )

Determine if an image driver has been set.

Returns:
boolean true if we have a driver, false otherwise.
    {
        return $this->_driver instanceof P4Cms_Image_Driver_Interface;
    }
P4Cms_Image::isSupportedType ( type)

Check if the given image type is supported.

Result is dependent on the driver. Return false if unable to get a driver.

Parameters:
string$typeimage type to check for
Returns:
bool true if given image type is supported by the driver, false otherwise; also return false if unable to get a driver
Exceptions:
InvalidArgumentExceptionif input $type is not a string
    {
        if (!is_string($type)) {
            throw new InvalidArgumentException("Type must be a string.");
        }

        // check if driver supports the type;
        // return false if we are unable to get a driver
        try {
            return $this->getDriver()->isSupportedType($type);
        } catch (P4Cms_Image_Exception $e) {
            return false;
        }
    }
P4Cms_Image::setData ( data = null)

Set the data of the image.

Parameters:
string | null$dataoptional - the image data to use
Returns:
P4Cms_Image provides fluent interface
    {
        if (isset($data) && !is_string($data)) {
            throw new InvalidArgumentException(
                'Data has to be a string or null.'
            );
        }
        $this->getDriver()->setData($data);

        return $this;
    }
P4Cms_Image::setDriver ( P4Cms_Image_Driver_Interface driver = null)

Set the graphics library to use.

Parameters:
P4Cms_Image_Driver_Interface | null$driveroptional - the image library driver to use
Returns:
P4Cms_Image provides fluent interface
Exceptions:
P4Cms_Image_Exceptionif the driver is not valid
    {
        // if we are setting a non-null driver, process pending transformations
        // and set the data on the new driver with the result
        if ($driver) {
            $data = $this->hasDriver() ? $this->getData() : null;
            $driver->setData($data);
        }

        $this->_driver = $driver;
    }
P4Cms_Image::transform ( transform,
args = array() 
)

Push the given transformation on to the queue.

Parameters:
string$transformname of the transformation
array$argsoptional - transformation arguments
Exceptions:
P4Cms_Image_Exceptionif given transformation is not supported
Returns:
P4Cms_Image provides fluent interface
    {
        // build a queue if neccessary
        if (!$this->_transforms) {
            $this->_transforms = new SplQueue();
        }

        // get the driver and make sure the transform is supported
        $driver = $this->getDriver();
        if (!$driver->isSupportedTransform($transform)) {
            throw new P4Cms_Image_Exception(
                'Transform "' . $transform . '" is not supported.'
            );
        }

        // queue the transform
        $this->_transforms->enqueue(array($transform, $args));
        return $this;
    }

Member Data Documentation

P4Cms_Image::$_driver = null [protected]
P4Cms_Image::$_transforms = null [protected]

The documentation for this class was generated from the following file: