Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Image_Driver_Gd Class Reference

Implementation of P4Cms_Image_Driver_Interface using the 'gd' extension. More...

Inheritance diagram for P4Cms_Image_Driver_Gd:
P4Cms_Image_Driver_Abstract P4Cms_Image_Driver_Interface

List of all members.

Public Member Functions

 getData ($type=null)
 Return binary image data.
 hasData ()
 Check if there are image data to operate with.
 setData ($data=null)
 Set the image data.

Static Public Member Functions

static isSupportedType ($type)
 Check if given image type is supported.

Protected Member Functions

 _crop ($width=null, $height=null, $x=0, $y=0)
 Crop the image to the given size and position.
 _getImageHeight ()
 Return image height in pixels.
 _getImageWidth ()
 Return image width in pixels.
 _getType ($imageData)
 Detect image type from a given image data.
 _rotate ($degrees=0)
 Rotate the image.
 _scale ($width=null, $height=null)
 Scale the image to the given size.
 _sharpen ()
 Sharpen the image by applying a default 3x3 matrix passed to the imageconvolution() function.

Protected Attributes

 $_resource = null
 $_type = null

Static Protected Attributes

static $_requiredExtension = 'gd'
static $_supportedTransforms

Detailed Description

Implementation of P4Cms_Image_Driver_Interface using the 'gd' extension.

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_Driver_Gd::_crop ( width = null,
height = null,
x = 0,
y = 0 
) [protected]

Crop the image to the given size and position.

Parameters:
int | null$widththe width in pixels
int | null$heightthe height in pixels
int$xthe x coordinate starting position
int$ythe y coordinate starting position
    {
        if (!$width || !$height) {
            throw new P4Cms_Image_Exception(
                'Both width and height are required.'
            );
        }

        // crop the image
        $dst = imagecreatetruecolor($width, $height);
        imagecopy(
            $dst,
            $this->_resource,
            0,
            0,
            $x,
            $y,
            $width,
            $height
        );
        $this->_resource = $dst;
    }
P4Cms_Image_Driver_Gd::_getImageHeight ( ) [protected]

Return image height in pixels.

Returns:
int image height in pixels

Reimplemented from P4Cms_Image_Driver_Abstract.

    {
        return imagesy($this->_resource);
    }
P4Cms_Image_Driver_Gd::_getImageWidth ( ) [protected]

Return image width in pixels.

Returns:
int image width in pixels

Reimplemented from P4Cms_Image_Driver_Abstract.

    {
        return imagesx($this->_resource);
    }
P4Cms_Image_Driver_Gd::_getType ( imageData) [protected]

Detect image type from a given image data.

Parameters:
string$imageDatadata representing image to detect type on
Returns:
string image type (jpeg, gif etc.)
Exceptions:
P4Cms_Image_Exceptionif image type cannot be determined
    {
        // try to get image type from 'finfo' if its available
        if (class_exists('finfo')) {
            $finfo = new finfo(FILEINFO_MIME_TYPE);
            $mime  = $finfo->buffer($imageData);

            return str_replace('image/', '', $mime);
        }

        throw new P4Cms_Image_Exception('Cannot determine image type.');
    }
P4Cms_Image_Driver_Gd::_rotate ( degrees = 0) [protected]

Rotate the image.

Parameters:
float$degreesthe rotation angle
    {
        $this->_resource = imagerotate($this->_resource, $degrees, 0);
    }
P4Cms_Image_Driver_Gd::_scale ( width = null,
height = null 
) [protected]

Scale the image to the given size.

Parameters:
int$widththe width in pixels
int$heightthe height in pixels
    {
        if (!$width && !$height) {
            throw new P4Cms_Image_Exception(
                'At least one of width or height is required.'
            );
        }

        // calculate original image aspect ratio
        $sourceWidth  = $this->_getImageWidth();
        $sourceHeight = $this->_getImageHeight();
        $ratio        = $sourceWidth / $sourceHeight;

        // if only one dimension is given, calculate the another one such
        // that new image keeps same ratio as the original
        if (!$width) {
            $width  = round($height * $ratio);
        } else if (!$height) {
            $height = round($width / $ratio);
        }

        // resize the image
        $dst = imagecreatetruecolor($width, $height);
        imagecopyresized(
            $dst,
            $this->_resource,
            0,
            0,
            0,
            0,
            $width,
            $height,
            $sourceWidth,
            $sourceHeight
        );
        $this->_resource = $dst;
    }
P4Cms_Image_Driver_Gd::_sharpen ( ) [protected]

Sharpen the image by applying a default 3x3 matrix passed to the imageconvolution() function.

    {
        $matrix  = array(
            array( 0.0, -1.0,  0.0),
            array(-1.0,  5.0, -1.0),
            array( 0.0, -1.0,  0.0)
        );
        $divisor = array_sum(array_map('array_sum', $matrix));

        // sharpen image by using image convolution
        imageconvolution(
            $this->_resource,
            $matrix,
            $divisor,
            0
        );
    }
P4Cms_Image_Driver_Gd::getData ( type = null)

Return binary image data.

Parameters:
string$typeoptional - the image format (will return image data in the same format as input if not provided)
Returns:
string|null binary image data or null if no image data were set

Implements P4Cms_Image_Driver_Interface.

    {
        // early exit if there are no image data
        if (!$this->hasData()) {
            return null;
        }

        // if no image type was provided, use the type of input image
        $type = $type ?: $this->_type;

        // check if given type is supported
        if (!static::isSupportedType($type)) {
            throw new P4Cms_Image_Exception("Image type '$type' is not supported.");
        }

        // assemble callback to get the image data in a given type
        // @todo should we normalize jpg/jpeg as there is imagejpeg() function but no imagejpg()
        $callback = 'image' . strtolower($type);

        // get image data
        ob_start();
        call_user_func($callback, $this->_resource);
        $data = ob_get_contents();
        ob_end_clean();

        return $data;
    }
P4Cms_Image_Driver_Gd::hasData ( )

Check if there are image data to operate with.

Returns:
bool true if there has been image data set, false otherwise.

Implements P4Cms_Image_Driver_Interface.

    {
        return $this->_resource !== null;
    }
static P4Cms_Image_Driver_Gd::isSupportedType ( type) [static]

Check if given image type is supported.

Parameters:
string$typeimage type to check for
Returns:
bool true if given image type is supported, false otherwise

Implements P4Cms_Image_Driver_Interface.

    {
        $constant = 'IMG_' . strtoupper($type);
        return defined($constant) && (imagetypes() & constant($constant));
    }
P4Cms_Image_Driver_Gd::setData ( data = null)

Set the image data.

Parameters:
string | null$dataoptional - image data
Returns:
P4Cms_Image_Driver_Gd provides fluent interface

Implements P4Cms_Image_Driver_Interface.

    {
        if ($data) {
            $this->_resource = imagecreatefromstring($data);
            $this->_type     = $this->_getType($data);
        } else {
            $this->_resource = null;
            $this->_type     = null;
        }
        return $this;
    }

Member Data Documentation

P4Cms_Image_Driver_Gd::$_requiredExtension = 'gd' [static, protected]

Reimplemented from P4Cms_Image_Driver_Abstract.

P4Cms_Image_Driver_Gd::$_resource = null [protected]
P4Cms_Image_Driver_Gd::$_supportedTransforms [static, protected]
Initial value:
 array(
        'scale',
        'sharpen',
        'crop',
        'rotate'
    )

Reimplemented from P4Cms_Image_Driver_Abstract.

P4Cms_Image_Driver_Gd::$_type = null [protected]

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