Source for file image.php
Documentation is available at image.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Class to manipulate an image.
* @package Joomla.Platform
* @var resource The image resource handle.
* @var string The source image path.
* @var array Whether or not different image formats are supported.
protected static $formats =
array();
* @param mixed $source Either a file path for a source image or a GD resource handler for an image.
* @throws RuntimeException
// Verify that GD support for PHP is available.
// @codeCoverageIgnoreStart
JLog::add('The GD extension for PHP is not available.', JLog::ERROR);
throw
new RuntimeException('The GD extension for PHP is not available.');
// @codeCoverageIgnoreEnd
// Determine which image types are supported by GD, but only once.
if (!isset
(self::$formats[IMAGETYPE_JPEG]))
self::$formats[IMAGETYPE_JPEG] =
($info['JPEG Support']) ?
true :
false;
self::$formats[IMAGETYPE_PNG] =
($info['PNG Support']) ?
true :
false;
self::$formats[IMAGETYPE_GIF] =
($info['GIF Read Support']) ?
true :
false;
// If the source input is a resource, set it as the image handle.
elseif (!empty($source) &&
is_string($source))
// If the source input is not empty, assume it is a path and populate the image handle.
* Method to return a properties object for an image given a filesystem path. The
* result object has values for image width, height, type, attributes, mime type, bits,
* @param string $path The filesystem path to the image for which to get properties.
* @throws InvalidArgumentException
* @throws RuntimeException
// Make sure the file exists.
throw
new InvalidArgumentException('The image file does not exist.');
// Get the image file information.
// @codeCoverageIgnoreStart
throw
new RuntimeException('Unable to get properties for the image.');
// @codeCoverageIgnoreEnd
// Build the response object.
$properties = (object)
array(
'attributes' =>
$info[3],
'bits' => isset
($info['bits']) ?
$info['bits'] :
null,
'channels' => isset
($info['channels']) ?
$info['channels'] :
null,
* Method to generate thumbnails from the current image. It allows
* creation by resizing or cropping the original image.
* @param mixed $thumbSizes String or array of strings. Example: $thumbSizes = array('150x75','250x150');
* @param integer $creationMethod 1-3 resize $scaleMethod | 4 create croppping | 5 resize then crop
* @throws InvalidArgumentException
public function generateThumbs($thumbSizes, $creationMethod =
self::SCALE_INSIDE)
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
// Accept a single thumbsize string as parameter
$thumbSizes =
array($thumbSizes);
foreach ($thumbSizes as $thumbSize)
// Desired thumbnail size
throw
new InvalidArgumentException('Invalid thumb size received: ' .
$thumbSize);
$thumb =
$this->crop($thumbWidth, $thumbHeight, null, null, true);
// Case for self::CROP_RESIZE
$thumb =
$this->cropResize($thumbWidth, $thumbHeight, true);
$thumb =
$this->resize($thumbWidth, $thumbHeight, true, $creationMethod);
// Store the thumb in the results array
* Method to create thumbnails from the current image and save them to disk. It allows creation by resizing
* or croppping the original image.
* @param mixed $thumbSizes string or array of strings. Example: $thumbSizes = array('150x75','250x150');
* @param integer $creationMethod 1-3 resize $scaleMethod | 4 create croppping
* @param string $thumbsFolder destination thumbs folder. null generates a thumbs folder in the image folder
* @throws InvalidArgumentException
public function createThumbs($thumbSizes, $creationMethod =
self::SCALE_INSIDE, $thumbsFolder =
null)
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
// No thumbFolder set -> we will create a thumbs folder in the current image folder
throw
new InvalidArgumentException('Folder does not exist and cannot be created: ' .
$thumbsFolder);
$thumbsCreated =
array();
// Parent image properties
$imgProperties =
self::getImageFileProperties($this->getPath());
foreach ($thumbs as $thumb)
$thumbWidth =
$thumb->getWidth();
$thumbHeight =
$thumb->getHeight();
$thumbFileName =
$filename .
'_' .
$thumbWidth .
'x' .
$thumbHeight .
'.' .
$fileExtension;
// Save thumb file to disk
$thumbFileName =
$thumbsFolder .
'/' .
$thumbFileName;
if ($thumb->toFile($thumbFileName, $imgProperties->type))
// Return JImage object with thumb path to ease further manipulation
$thumb->path =
$thumbFileName;
$thumbsCreated[] =
$thumb;
* Method to crop the current image.
* @param mixed $width The width of the image section to crop in pixels or a percentage.
* @param mixed $height The height of the image section to crop in pixels or a percentage.
* @param integer $left The number of pixels from the left to start cropping.
* @param integer $top The number of pixels from the top to start cropping.
* @param boolean $createNew If true the current image will be cloned, cropped and returned; else
* the current image will be cropped and returned.
public function crop($width, $height, $left =
null, $top =
null, $createNew =
true)
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
// Create the new truecolor image handle.
// Allow transparency for the new image handle.
// Get the transparent color values for the current image.
$color =
imageColorAllocate($this->handle, $rgba['red'], $rgba['green'], $rgba['blue']);
// Set the transparent color values for the new image.
// If we are cropping to a new image, create a new JImage object.
// @codeCoverageIgnoreStart
// @codeCoverageIgnoreEnd
// Swap out the current handle for the new image handle.
// Free the memory from the current handle
* Method to apply a filter to the image by type. Two examples are: grayscale and sketchy.
* @param string $type The name of the image filter to apply.
* @param array $options An array of options for the filter.
public function filter($type, array $options =
array())
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
// Get the image filter instance.
// Execute the image filter.
$filter->execute($options);
* Method to get the height of the image in pixels.
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
* Method to get the width of the image in pixels.
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
* Method to return the path
* Method to determine whether or not an image has been loaded into the object.
// Make sure the resource handle is valid.
* Method to determine whether or not the image has transparency.
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
* Method to load a file into the JImage object as the resource.
* @param string $path The filesystem path to load as an image.
* @throws InvalidArgumentException
* @throws RuntimeException
// Destroy the current image handle if it exists
// Make sure the file exists.
throw
new InvalidArgumentException('The image file does not exist.');
// Get the image properties.
$properties =
self::getImageFileProperties($path);
// Attempt to load the image based on the MIME-Type
switch ($properties->mime)
// Make sure the image type is supported.
if (empty(self::$formats[IMAGETYPE_GIF]))
// @codeCoverageIgnoreStart
JLog::add('Attempting to load an image of unsupported type GIF.', JLog::ERROR);
throw
new RuntimeException('Attempting to load an image of unsupported type GIF.');
// @codeCoverageIgnoreEnd
// Attempt to create the image handle.
// @codeCoverageIgnoreStart
throw
new RuntimeException('Unable to process GIF image.');
// @codeCoverageIgnoreEnd
// Make sure the image type is supported.
if (empty(self::$formats[IMAGETYPE_JPEG]))
// @codeCoverageIgnoreStart
JLog::add('Attempting to load an image of unsupported type JPG.', JLog::ERROR);
throw
new RuntimeException('Attempting to load an image of unsupported type JPG.');
// @codeCoverageIgnoreEnd
// Attempt to create the image handle.
// @codeCoverageIgnoreStart
throw
new RuntimeException('Unable to process JPG image.');
// @codeCoverageIgnoreEnd
// Make sure the image type is supported.
if (empty(self::$formats[IMAGETYPE_PNG]))
// @codeCoverageIgnoreStart
JLog::add('Attempting to load an image of unsupported type PNG.', JLog::ERROR);
throw
new RuntimeException('Attempting to load an image of unsupported type PNG.');
// @codeCoverageIgnoreEnd
// Attempt to create the image handle.
// @codeCoverageIgnoreStart
throw
new RuntimeException('Unable to process PNG image.');
// @codeCoverageIgnoreEnd
// Set transparency for non-transparent PNGs.
// Assign to black which is default for transparent PNGs
JLog::add('Attempting to load an image of unsupported type: ' .
$properties->mime, JLog::ERROR);
throw
new InvalidArgumentException('Attempting to load an image of unsupported type: ' .
$properties->mime);
// Set the filesystem path to the source image.
* Method to resize the current image.
* @param mixed $width The width of the resized image in pixels or a percentage.
* @param mixed $height The height of the resized image in pixels or a percentage.
* @param boolean $createNew If true the current image will be cloned, resized and returned; else
* the current image will be resized and returned.
* @param integer $scaleMethod Which method to use for scaling
public function resize($width, $height, $createNew =
true, $scaleMethod =
self::SCALE_INSIDE)
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
// Prepare the dimensions for the resize operation.
$offset->x =
$offset->y =
0;
// Center image if needed and create the new truecolor image handle.
if ($scaleMethod ==
self::SCALE_FIT)
$offset->x =
round(($width -
$dimensions->width) /
2);
$offset->y =
round(($height -
$dimensions->height) /
2);
// Make image transparent, otherwise cavas outside initial image would default to black
$transparency =
imagecolorAllocateAlpha($this->handle, 0, 0, 0, 127);
imagecolorTransparent($this->handle, $transparency);
// Allow transparency for the new image handle.
// Get the transparent color values for the current image.
$color =
imageColorAllocateAlpha($this->handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
// Set the transparent color values for the new image.
// If we are resizing to a new image, create a new JImage object.
// @codeCoverageIgnoreStart
// @codeCoverageIgnoreEnd
// Swap out the current handle for the new image handle.
// Free the memory from the current handle
* Method to crop an image after resizing it to maintain
* proportions without having to do all the set up work.
* @param integer $width The desired width of the image in pixels or a percentage.
* @param integer $height The desired height of the image in pixels or a percentage.
* @param integer $createNew If true the current image will be cloned, resized, cropped and returned.
* @return object JImage Object for chaining.
public function cropResize($width, $height, $createNew =
true)
$this->resize($width, 0, false);
$this->resize(0, $height, false);
return $this->crop($width, $height, null, null, $createNew);
* Method to rotate the current image.
* @param mixed $angle The angle of rotation for the image
* @param integer $background The background color to use when areas are added due to rotation
* @param boolean $createNew If true the current image will be cloned, rotated and returned; else
* the current image will be rotated and returned.
public function rotate($angle, $background = -
1, $createNew =
true)
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
// Create the new truecolor image handle.
// Allow transparency for the new image handle.
// If we are resizing to a new image, create a new JImage object.
// @codeCoverageIgnoreStart
// @codeCoverageIgnoreEnd
// Swap out the current handle for the new image handle.
// Free the memory from the current handle
* Method to write the current image out to a file.
* @param string $path The filesystem path to save the image.
* @param integer $type The image type to save the file as.
* @param array $options The image type options to use in saving the file.
* @see http://www.php.net/manual/image.constants.php
public function toFile($path, $type =
IMAGETYPE_JPEG, array $options =
array())
// Make sure the resource handle is valid.
throw
new LogicException('No valid image was loaded.');
* Method to get an image filter instance of a specified type.
* @param string $type The image filter type to get.
* @throws RuntimeException
// Sanitize the filter type.
// Verify that the filter type exists.
$className =
'JImageFilter' .
ucfirst($type);
throw
new RuntimeException('The ' .
ucfirst($type) .
' image filter is not available.');
// Instantiate the filter object.
$instance =
new $className($this->handle);
// Verify that the filter type is valid.
// @codeCoverageIgnoreStart
throw
new RuntimeException('The ' .
ucfirst($type) .
' image filter is not valid.');
// @codeCoverageIgnoreEnd
* Method to get the new dimensions for a resized image.
* @param integer $width The width of the resized image in pixels.
* @param integer $height The height of the resized image in pixels.
* @param integer $scaleMethod The method to use for scaling
* @throws InvalidArgumentException If width, height or both given as zero
// Instantiate variables.
$dimensions =
new stdClass;
$dimensions->width = (int)
round($width);
$dimensions->height = (int)
round($height);
case self::SCALE_OUTSIDE:
$rx =
($width >
0) ?
($this->getWidth() /
$width) :
0;
$ry =
($height >
0) ?
($this->getHeight() /
$height) :
0;
if ($scaleMethod !=
self::SCALE_OUTSIDE)
throw
new InvalidArgumentException('Invalid scale method.');
* Method to sanitize a height value.
* @param mixed $height The input height value to sanitize.
* @param mixed $width The input width value for reference.
// If no height was given we will assume it is a square and use the width.
$height =
($height ===
null) ?
$width :
$height;
// If we were given a percentage, calculate the integer value.
if (preg_match('/^[0-9]+(\.[0-9]+)?\%$/', $height))
// Else do some rounding so we come out with a sane integer value.
$height = (int)
round((float)
$height);
* Method to sanitize an offset value like left or top.
* @param mixed $offset An offset value.
return (int)
round((float)
$offset);
* Method to sanitize a width value.
* @param mixed $width The input width value to sanitize.
* @param mixed $height The input height value for reference.
// If no width was given we will assume it is a square and use the height.
$width =
($width ===
null) ?
$height :
$width;
// If we were given a percentage, calculate the integer value.
if (preg_match('/^[0-9]+(\.[0-9]+)?\%$/', $width))
// Else do some rounding so we come out with a sane integer value.
$width = (int)
round((float)
$width);
* Method to destroy an image handle and
* free the memory associated with the handle
* @return boolean True on success, false on failure or if no image is loaded
* Method to call the destroy() method one last time
* to free any memory when the object is unset
Documentation generated on Tue, 19 Nov 2013 15:05:16 +0100 by phpDocumentor 1.4.3