- md5
- e7d32b8818c4599d9139ca714de6237d
- sha1
- 704373b3dbbb2bf4de346df18571aaa35a5b934a
- sha256
- fe080b176bcdd1b816931df7b6574e48ab1d9c85196b803f6c662256339e1670
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
<?php
/**
* @package Joomla.Platform
* @subpackage Image
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\CMS\Image;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Log\Log;
use Joomla\Image\Image as FrameworkImage;
use Joomla\Image\ImageFilter;
/**
* Class to manipulate an image.
*
* @since 1.7.3
*/
class Image extends FrameworkImage
{
/**
* True for best quality. False for speed
*
* @var boolean
* @since 3.7.0
*/
protected $generateBestQuality = true;
/**
* Class constructor.
*
* @param mixed $source Either a file path for a source image or a GD resource handler for an image.
*
* @since 1.7.3
* @throws \RuntimeException
*/
public function __construct($source = null)
{
// Inject the PSR-3 compatible logger in for forward compatibility
$this->setLogger(Log::createDelegatedLogger());
parent::__construct($source);
}
/**
* 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.
*
* @return Image
*
* @since 1.7.3
* @throws \LogicException
*/
public function crop($width, $height, $left = null, $top = null, $createNew = true)
{
// Make sure the resource handle is valid.
if (!$this->isLoaded())
{
throw new \LogicException('No valid image was loaded.');
}
// Sanitize width.
$width = $this->sanitizeWidth($width, $height);
// Sanitize height.
$height = $this->sanitizeHeight($height, $width);
// Autocrop offsets
if (\is_null($left))
{
$left = round(($this->getWidth() - $width) / 2);
}
if (\is_null($top))
{
$top = round(($this->getHeight() - $height) / 2);
}
// Sanitize left.
$left = $this->sanitizeOffset($left);
// Sanitize top.
$top = $this->sanitizeOffset($top);
// Create the new truecolor image handle.
$handle = imagecreatetruecolor($width, $height);
// Allow transparency for the new image handle.
imagealphablending($handle, false);
imagesavealpha($handle, true);
if ($this->isTransparent())
{
// Get the transparent color values for the current image.
$rgba = imageColorsForIndex($this->handle, imagecolortransparent($this->handle));
$color = imageColorAllocateAlpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
// Set the transparent color values for the new image.
imagecolortransparent($handle, $color);
imagefill($handle, 0, 0, $color);
}
if (!$this->generateBestQuality)
{
imagecopyresized($handle, $this->handle, 0, 0, $left, $top, $width, $height, $width, $height);
}
else
{
imagecopyresampled($handle, $this->handle, 0, 0, $left, $top, $width, $height, $width, $height);
}
// If we are cropping to a new image, create a new JImage object.
if ($createNew)
{
// @codeCoverageIgnoreStart
$new = new Image($handle);
return $new;
// @codeCoverageIgnoreEnd
}
// Swap out the current handle for the new image handle.
else
{
// Free the memory from the current handle
$this->destroy();
$this->handle = $handle;
return $this;
}
}
/**
* 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
*
* @return Image
*
* @since 1.7.3
* @throws \LogicException
*/
public function resize($width, $height, $createNew = true, $scaleMethod = self::SCALE_INSIDE)
{
// Make sure the resource handle is valid.
if (!$this->isLoaded())
{
throw new \LogicException('No valid image was loaded.');
}
// Sanitize width.
$width = $this->sanitizeWidth($width, $height);
// Sanitize height.
$height = $this->sanitizeHeight($height, $width);
// Prepare the dimensions for the resize operation.
$dimensions = $this->prepareDimensions($width, $height, $scaleMethod);
// Instantiate offset.
$offset = new \stdClass;
$offset->x = $offset->y = 0;
// Center image if needed and create the new truecolor image handle.
if ($scaleMethod == self::SCALE_FIT)
{
// Get the offsets
$offset->x = round(($width - $dimensions->width) / 2);
$offset->y = round(($height - $dimensions->height) / 2);
$handle = imagecreatetruecolor($width, $height);
// Make image transparent, otherwise cavas outside initial image would default to black
if (!$this->isTransparent())
{
$transparency = imagecolorAllocateAlpha($this->handle, 0, 0, 0, 127);
imagecolorTransparent($this->handle, $transparency);
}
}
else
{
$handle = imagecreatetruecolor($dimensions->width, $dimensions->height);
}
// Allow transparency for the new image handle.
imagealphablending($handle, false);
imagesavealpha($handle, true);
if ($this->isTransparent())
{
// Get the transparent color values for the current image.
$rgba = imageColorsForIndex($this->handle, imagecolortransparent($this->handle));
$color = imageColorAllocateAlpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
// Set the transparent color values for the new image.
imagecolortransparent($handle, $color);
imagefill($handle, 0, 0, $color);
}
if (!$this->generateBestQuality)
{
imagecopyresized(
$handle,
$this->handle,
$offset->x,
$offset->y,
0,
0,
$dimensions->width,
$dimensions->height,
$this->getWidth(),
$this->getHeight()
);
}
else
{
imagecopyresampled(
$handle,
$this->handle,
$offset->x,
$offset->y,
0,
0,
$dimensions->width,
$dimensions->height,
$this->getWidth(),
$this->getHeight()
);
}
// If we are resizing to a new image, create a new JImage object.
if ($createNew)
{
// @codeCoverageIgnoreStart
$new = new Image($handle);
return $new;
// @codeCoverageIgnoreEnd
}
// Swap out the current handle for the new image handle.
else
{
// Free the memory from the current handle
$this->destroy();
$this->handle = $handle;
return $this;
}
}
/**
* Method to get an image filter instance of a specified type.
*
* @param string $type The image filter type to get.
*
* @return ImageFilter
*
* @since 1.7.3
* @throws \RuntimeException
*/
protected function getFilterInstance($type)
{
// Sanitize the filter type.
$type = strtolower(preg_replace('#[^A-Z0-9_]#i', '', $type));
// Verify that the filter type exists.
$className = 'JImageFilter' . ucfirst($type);
if (!class_exists($className))
{
$className = __NAMESPACE__ . '\\Filter\\' . ucfirst($type);
if (!class_exists($className))
{
Log::add('The ' . ucfirst($type) . ' image filter is not available.', Log::ERROR);
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.
if (!($instance instanceof ImageFilter))
{
// @codeCoverageIgnoreStart
Log::add('The ' . ucfirst($type) . ' image filter is not valid.', Log::ERROR);
throw new \RuntimeException('The ' . ucfirst($type) . ' image filter is not valid.');
// @codeCoverageIgnoreEnd
}
return $instance;
}
/**
* Method for set option of generate thumbnail method
*
* @param boolean $quality True for best quality. False for best speed.
*
* @return void
*
* @since 3.7.0
*/
public function setThumbnailGenerate($quality = true)
{
$this->generateBestQuality = (boolean) $quality;
}
}
The same content is in 2 releases, Joomla 4.0.0-alpha11 to 4.0.0-alpha12. See them all, or every version of this file.