Implementation of P4Cms_Image_Driver_Interface using the 'gd' extension.
More...
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 | $width | the width in pixels |
int | null | $height | the height in pixels |
int | $x | the x coordinate starting position |
int | $y | the y coordinate starting position |
{
if (!$width || !$height) {
throw new P4Cms_Image_Exception(
'Both width and height are required.'
);
}
$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 | $imageData | data representing image to detect type on |
- Returns:
- string image type (jpeg, gif etc.)
- Exceptions:
-
{
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 | $degrees | the 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 | $width | the width in pixels |
int | $height | the height in pixels |
{
if (!$width && !$height) {
throw new P4Cms_Image_Exception(
'At least one of width or height is required.'
);
}
$sourceWidth = $this->_getImageWidth();
$sourceHeight = $this->_getImageHeight();
$ratio = $sourceWidth / $sourceHeight;
if (!$width) {
$width = round($height * $ratio);
} else if (!$height) {
$height = round($width / $ratio);
}
$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));
imageconvolution(
$this->_resource,
$matrix,
$divisor,
0
);
}
P4Cms_Image_Driver_Gd::getData |
( |
$ |
type = null | ) |
|
Return binary image data.
- Parameters:
-
string | $type | optional - 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.
{
if (!$this->hasData()) {
return null;
}
$type = $type ?: $this->_type;
if (!static::isSupportedType($type)) {
throw new P4Cms_Image_Exception("Image type '$type' is not supported.");
}
$callback = 'image' . strtolower($type);
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 | $type | image 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 | $data | optional - 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] |
P4Cms_Image_Driver_Gd::$_resource = null [protected] |
P4Cms_Image_Driver_Gd::$_supportedTransforms [static, protected] |
P4Cms_Image_Driver_Gd::$_type = null [protected] |
The documentation for this class was generated from the following file: