2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A wrapper class for GD-based images, with lots of manipulation functions.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage filesystem
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-10-25 00:28:39 +02:00
|
|
|
class GDBackend extends Object implements Image_Backend {
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $gd, $width, $height;
|
2008-06-25 07:22:56 +02:00
|
|
|
protected $quality;
|
2013-10-19 13:46:23 +02:00
|
|
|
protected $interlace;
|
2014-01-02 16:59:34 +01:00
|
|
|
protected $cache, $cacheKey, $manipulation;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private static $default_quality = 75;
|
2010-10-19 07:04:14 +02:00
|
|
|
|
2013-10-19 13:46:23 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private static $image_interlace = 0;
|
|
|
|
|
2010-10-19 07:04:14 +02:00
|
|
|
/**
|
|
|
|
* Set the default image quality.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "GDBackend.default_quality" config setting instead
|
2010-10-19 07:04:14 +02:00
|
|
|
* @param quality int A number from 0 to 100, 100 being the best quality.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_default_quality($quality) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "GDBackend.default_quality" config setting instead');
|
2010-10-19 07:04:14 +02:00
|
|
|
if(is_numeric($quality) && (int) $quality >= 0 && (int) $quality <= 100) {
|
2013-03-26 00:02:46 +01:00
|
|
|
config::inst()->update('GDBackend', 'default_quality', (int) $quality);
|
2010-10-19 07:04:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:58:47 +01:00
|
|
|
/**
|
|
|
|
* __construct
|
|
|
|
*
|
|
|
|
* @param string $filename = null
|
|
|
|
* @param array $args = array()
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-01-02 16:59:34 +01:00
|
|
|
public function __construct($filename = null, $args = array()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// If we're working with image resampling, things could take a while. Bump up the time-limit
|
2009-06-28 04:36:46 +02:00
|
|
|
increase_time_limit_to(300);
|
2008-11-17 23:59:17 +01:00
|
|
|
|
2014-01-02 16:59:34 +01:00
|
|
|
$this->cache = SS_Cache::factory('GDBackend_Manipulations');
|
|
|
|
|
2014-12-11 16:21:38 +01:00
|
|
|
if($filename && is_readable($filename)) {
|
2014-01-02 16:59:34 +01:00
|
|
|
$this->cacheKey = md5(implode('_', array($filename, filemtime($filename))));
|
|
|
|
$this->manipulation = implode('|', $args);
|
|
|
|
|
|
|
|
$cacheData = unserialize($this->cache->load($this->cacheKey));
|
|
|
|
$cacheData = ($cacheData !== false) ? $cacheData : array();
|
|
|
|
|
|
|
|
if ($this->imageAvailable($filename, $this->manipulation)) {
|
|
|
|
$cacheData[$this->manipulation] = true;
|
|
|
|
$this->cache->save(serialize($cacheData), $this->cacheKey);
|
|
|
|
|
|
|
|
// We use getimagesize instead of extension checking, because sometimes extensions are wrong.
|
|
|
|
list($width, $height, $type, $attr) = getimagesize($filename);
|
|
|
|
switch($type) {
|
|
|
|
case 1:
|
|
|
|
if(function_exists('imagecreatefromgif'))
|
|
|
|
$this->setImageResource(imagecreatefromgif($filename));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if(function_exists('imagecreatefromjpeg'))
|
|
|
|
$this->setImageResource(imagecreatefromjpeg($filename));
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if(function_exists('imagecreatefrompng')) {
|
|
|
|
$img = imagecreatefrompng($filename);
|
|
|
|
imagesavealpha($img, true); // save alphablending setting (important)
|
|
|
|
$this->setImageResource($img);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
$this->quality = $this->config()->default_quality;
|
2013-10-19 13:46:23 +02:00
|
|
|
$this->interlace = $this->config()->image_interlace;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function setImageResource($resource) {
|
|
|
|
$this->gd = $resource;
|
|
|
|
$this->width = imagesx($resource);
|
|
|
|
$this->height = imagesy($resource);
|
|
|
|
}
|
2010-10-19 07:04:00 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function getImageResource() {
|
|
|
|
return $this->gd;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-10-19 07:04:00 +02:00
|
|
|
|
2014-01-02 16:59:34 +01:00
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function imageAvailable($filename, $manipulation) {
|
|
|
|
return ($this->checkAvailableMemory($filename) && ! $this->failedResample($filename, $manipulation));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we've got enough memory available for resampling this image. This check is rough,
|
|
|
|
* so it will not catch all images that are too large - it also won't work accurately on large,
|
|
|
|
* animated GIFs as bits per pixel can't be calculated for an animated GIF with a global color
|
|
|
|
* table.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-01-02 16:59:34 +01:00
|
|
|
* @param string $filename
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function checkAvailableMemory($filename) {
|
|
|
|
$limit = translate_memstring(ini_get('memory_limit'));
|
|
|
|
|
|
|
|
if ($limit < 0) return true; // memory_limit == -1
|
|
|
|
|
|
|
|
$imageInfo = getimagesize($filename);
|
|
|
|
|
|
|
|
// bits per channel (rounded up, default to 1)
|
|
|
|
$bits = isset($imageInfo['bits']) ? ($imageInfo['bits'] + 7) / 8 : 1;
|
|
|
|
|
|
|
|
// channels (default 4 rgba)
|
|
|
|
$channels = isset($imageInfo['channels']) ? $imageInfo['channels'] : 4;
|
|
|
|
|
|
|
|
$bytesPerPixel = $bits * $channels;
|
|
|
|
|
|
|
|
// width * height * bytes per pixel
|
|
|
|
$memoryRequired = $imageInfo[0] * $imageInfo[1] * $bytesPerPixel;
|
|
|
|
|
|
|
|
return $memoryRequired + memory_get_usage() < $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this image has previously crashed GD when attempting to open it - if it's opened
|
|
|
|
* successfully, the manipulation's cache key is removed.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-01-02 16:59:34 +01:00
|
|
|
* @param string $filename
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function failedResample($filename, $manipulation) {
|
|
|
|
$cacheData = unserialize($this->cache->load($this->cacheKey));
|
|
|
|
return ($cacheData && array_key_exists($manipulation, $cacheData));
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the image quality, used when saving JPEGs.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setQuality($quality) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->quality = $quality;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize an image to cover the given width/height completely, and crop off any overhanging edges.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function croppedResize($width, $height) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->gd) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$width = round($width);
|
|
|
|
$height = round($height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-26 06:21:14 +02:00
|
|
|
// Check that a resize is actually necessary.
|
|
|
|
if ($width == $this->width && $height == $this->height) {
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$destAR = $width / $height;
|
|
|
|
if ($this->width > 0 && $this->height > 0 ){
|
|
|
|
// We can't divide by zero theres something wrong.
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$srcAR = $this->width / $this->height;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Destination narrower than the source
|
|
|
|
if($destAR < $srcAR) {
|
|
|
|
$srcY = 0;
|
|
|
|
$srcHeight = $this->height;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$srcWidth = round( $this->height * $destAR );
|
|
|
|
$srcX = round( ($this->width - $srcWidth) / 2 );
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Destination shorter than the source
|
|
|
|
} else {
|
|
|
|
$srcX = 0;
|
|
|
|
$srcWidth = $this->width;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$srcHeight = round( $this->width / $destAR );
|
|
|
|
$srcY = round( ($this->height - $srcHeight) / 2 );
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
imagecopyresampled($newGD, $this->gd, 0,0, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);
|
2010-05-25 05:53:12 +02:00
|
|
|
}
|
|
|
|
$output = clone $this;
|
2012-10-25 00:28:39 +02:00
|
|
|
$output->setImageResource($newGD);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resizes the image to fit within the given region.
|
|
|
|
* Behaves similarly to paddedResize but without the padding.
|
|
|
|
* @todo This method isn't very efficent
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fittedResize($width, $height) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$gd = $this->resizeByHeight($height);
|
|
|
|
if($gd->width > $width) $gd = $gd->resizeByWidth($width);
|
|
|
|
return $gd;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
/**
|
|
|
|
* hasImageResource
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasImageResource() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->gd ? true : false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize an image, skewing it as necessary.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resize($width, $height) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->gd) return;
|
2014-10-30 00:30:51 +01:00
|
|
|
|
|
|
|
if($width < 0 || $height < 0) throw new InvalidArgumentException("Image resizing dimensions cannot be negative");
|
|
|
|
if(!$width && !$height) throw new InvalidArgumentException("No dimensions given when resizing image");
|
|
|
|
if(!$width) throw new InvalidArgumentException("Width not given when resizing image");
|
|
|
|
if(!$height) throw new InvalidArgumentException("Height not given when resizing image");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2014-10-30 00:30:51 +01:00
|
|
|
//use whole numbers, ensuring that size is at least 1x1
|
|
|
|
$width = max(1, round($width));
|
|
|
|
$height = max(1, round($height));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-26 06:21:14 +02:00
|
|
|
// Check that a resize is actually necessary.
|
|
|
|
if ($width == $this->width && $height == $this->height) {
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
|
|
|
|
|
|
|
imagecopyresampled($newGD, $this->gd, 0,0, 0, 0, $width, $height, $this->width, $this->height);
|
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2012-10-25 00:28:39 +02:00
|
|
|
$output->setImageResource($newGD);
|
2007-09-14 03:48:38 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-14 03:51:53 +02:00
|
|
|
/**
|
|
|
|
* Rotates image by given angle.
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param angle
|
|
|
|
*
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function rotate($angle) {
|
2007-09-14 03:48:38 +02:00
|
|
|
if(!$this->gd) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
if(function_exists("imagerotate")) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$newGD = imagerotate($this->gd, $angle,0);
|
2007-09-14 03:52:50 +02:00
|
|
|
} else {
|
2012-12-08 12:20:20 +01:00
|
|
|
//imagerotate is not included in PHP included in Ubuntu
|
2014-08-15 08:53:05 +02:00
|
|
|
$newGD = $this->rotatePixelByPixel($angle);
|
2007-09-14 03:52:50 +02:00
|
|
|
}
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2012-10-25 00:28:39 +02:00
|
|
|
$output->setImageResource($newGD);
|
2007-09-14 03:48:38 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-14 03:52:50 +02:00
|
|
|
/**
|
2012-12-08 12:20:20 +01:00
|
|
|
* Rotates image by given angle. It's slow because makes it pixel by pixel rather than
|
|
|
|
* using built-in function. Used when imagerotate function is not available(i.e. Ubuntu)
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param angle
|
|
|
|
*
|
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
public function rotatePixelByPixel($angle) {
|
|
|
|
$sourceWidth = imagesx($this->gd);
|
|
|
|
$sourceHeight = imagesy($this->gd);
|
|
|
|
if ($angle == 180) {
|
|
|
|
$destWidth = $sourceWidth;
|
|
|
|
$destHeight = $sourceHeight;
|
|
|
|
} else {
|
|
|
|
$destWidth = $sourceHeight;
|
|
|
|
$destHeight = $sourceWidth;
|
|
|
|
}
|
|
|
|
$rotate=imagecreatetruecolor($destWidth,$destHeight);
|
|
|
|
imagealphablending($rotate, false);
|
|
|
|
for ($x = 0; $x < ($sourceWidth); $x++) {
|
|
|
|
for ($y = 0; $y < ($sourceHeight); $y++) {
|
|
|
|
$color = imagecolorat($this->gd, $x, $y);
|
|
|
|
switch ($angle) {
|
|
|
|
case 90:
|
|
|
|
imagesetpixel($rotate, $y, $destHeight - $x - 1, $color);
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
imagesetpixel($rotate, $destWidth - $x - 1, $destHeight - $y - 1, $color);
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
imagesetpixel($rotate, $destWidth - $y - 1, $x, $color);
|
|
|
|
break;
|
|
|
|
default: $rotate = $this->gd;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rotate;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2007-09-14 03:51:53 +02:00
|
|
|
/**
|
|
|
|
* Crop's part of image.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @param top y position of left upper corner of crop rectangle
|
2007-09-14 03:51:53 +02:00
|
|
|
* @param left x position of left upper corner of crop rectangle
|
|
|
|
* @param width rectangle width
|
|
|
|
* @param height rectangle height
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return GD
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function crop($top, $left, $width, $height) {
|
2007-09-14 03:48:38 +02:00
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
2013-03-11 23:19:09 +01:00
|
|
|
|
|
|
|
// Preserve alpha channel between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
|
|
|
|
2007-09-14 03:48:38 +02:00
|
|
|
imagecopyresampled($newGD, $this->gd, 0, 0, $left, $top, $width, $height, $width, $height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2012-10-25 00:28:39 +02:00
|
|
|
$output->setImageResource($newGD);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
2007-09-14 03:51:53 +02:00
|
|
|
* Method return width of image.
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2007-09-14 03:51:53 +02:00
|
|
|
* @return integer width.
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getWidth() {
|
2007-09-14 03:51:53 +02:00
|
|
|
return $this->width;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-14 03:51:53 +02:00
|
|
|
/**
|
|
|
|
* Method return height of image.
|
2008-06-25 07:22:56 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return integer height
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHeight() {
|
2007-09-14 03:51:53 +02:00
|
|
|
return $this->height;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize an image by width. Preserves aspect ratio.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resizeByWidth( $width ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$heightScale = $width / $this->width;
|
|
|
|
return $this->resize( $width, $heightScale * $this->height );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Resize an image by height. Preserves aspect ratio
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resizeByHeight( $height ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$scale = $height / $this->height;
|
|
|
|
return $this->resize( $scale * $this->width, $height );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Resize the image by preserving aspect ratio. By default, it will keep the image inside the maxWidth
|
|
|
|
* and maxHeight. Passing useAsMinimum will make the smaller dimension equal to the maximum corresponding dimension
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resizeRatio( $maxWidth, $maxHeight, $useAsMinimum = false ) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$widthRatio = $maxWidth / $this->width;
|
|
|
|
$heightRatio = $maxHeight / $this->height;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( $widthRatio < $heightRatio )
|
|
|
|
return $useAsMinimum ? $this->resizeByHeight( $maxHeight ) : $this->resizeByWidth( $maxWidth );
|
|
|
|
else
|
|
|
|
return $useAsMinimum ? $this->resizeByWidth( $maxWidth ) : $this->resizeByHeight( $maxHeight );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function color_web2gd($image, $webColor) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(substr($webColor,0,1) == "#") $webColor = substr($webColor,1);
|
|
|
|
$r = hexdec(substr($webColor,0,2));
|
|
|
|
$g = hexdec(substr($webColor,2,2));
|
|
|
|
$b = hexdec(substr($webColor,4,2));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return imagecolorallocate($image, $r, $g, $b);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize to fit fully within the given box, without resizing. Extra space left around
|
|
|
|
* the image will be padded with the background color.
|
2012-12-08 12:20:20 +01:00
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
* @param backgroundColour
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->gd) return;
|
|
|
|
$width = round($width);
|
|
|
|
$height = round($height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-26 06:21:14 +02:00
|
|
|
// Check that a resize is actually necessary.
|
|
|
|
if ($width == $this->width && $height == $this->height) {
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$newGD = imagecreatetruecolor($width, $height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$bg = GD::color_web2gd($newGD, $backgroundColor);
|
|
|
|
imagefilledrectangle($newGD, 0, 0, $width, $height, $bg);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$destAR = $width / $height;
|
|
|
|
if ($this->width > 0 && $this->height > 0) {
|
|
|
|
// We can't divide by zero theres something wrong.
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$srcAR = $this->width / $this->height;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Destination narrower than the source
|
|
|
|
if($destAR > $srcAR) {
|
|
|
|
$destY = 0;
|
|
|
|
$destHeight = $height;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$destWidth = round( $height * $srcAR );
|
|
|
|
$destX = round( ($width - $destWidth) / 2 );
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Destination shorter than the source
|
|
|
|
} else {
|
|
|
|
$destX = 0;
|
|
|
|
$destWidth = $width;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-30 03:49:44 +02:00
|
|
|
$destHeight = round( $width / $srcAR );
|
|
|
|
$destY = round( ($height - $destHeight) / 2 );
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
imagecopyresampled($newGD, $this->gd,
|
|
|
|
$destX, $destY, 0, 0,
|
|
|
|
$destWidth, $destHeight, $this->width, $this->height);
|
2010-05-25 05:53:12 +02:00
|
|
|
}
|
|
|
|
$output = clone $this;
|
2012-10-25 00:28:39 +02:00
|
|
|
$output->setImageResource($newGD);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the image greyscale
|
|
|
|
* $rv = red value, defaults to 38
|
|
|
|
* $gv = green value, defaults to 36
|
|
|
|
* $bv = blue value, defaults to 26
|
2012-09-26 23:34:00 +02:00
|
|
|
* Based (more or less entirely, with changes for readability) on code from
|
|
|
|
* http://www.teckis.com/scriptix/thumbnails/teck.html
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function greyscale($rv=38, $gv=36, $bv=26) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$width = $this->width;
|
|
|
|
$height = $this->height;
|
|
|
|
$newGD = imagecreatetruecolor($this->width, $this->height);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Preserves transparency between images
|
|
|
|
imagealphablending($newGD, false);
|
|
|
|
imagesavealpha($newGD, true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$rt = $rv + $bv + $gv;
|
|
|
|
$rr = ($rv == 0) ? 0 : 1/($rt/$rv);
|
|
|
|
$br = ($bv == 0) ? 0 : 1/($rt/$bv);
|
|
|
|
$gr = ($gv == 0) ? 0 : 1/($rt/$gv);
|
2009-04-29 01:52:15 +02:00
|
|
|
for($dy = 0; $dy < $height; $dy++) {
|
|
|
|
for($dx = 0; $dx < $width; $dx++) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$pxrgb = imagecolorat($this->gd, $dx, $dy);
|
|
|
|
$heightgb = ImageColorsforIndex($this->gd, $pxrgb);
|
|
|
|
$newcol = ($rr*$heightgb['red']) + ($br*$heightgb['blue']) + ($gr*$heightgb['green']);
|
2012-12-04 05:15:23 +01:00
|
|
|
$setcol = ImageColorAllocateAlpha($newGD, $newcol, $newcol, $newcol, $heightgb['alpha']);
|
2007-07-19 12:40:28 +02:00
|
|
|
imagesetpixel($newGD, $dx, $dy, $setcol);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 05:53:12 +02:00
|
|
|
$output = clone $this;
|
2012-10-25 00:28:39 +02:00
|
|
|
$output->setImageResource($newGD);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function makeDir($dirname) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!file_exists(dirname($dirname))) $this->makeDir(dirname($dirname));
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!file_exists($dirname)) mkdir($dirname, Config::inst()->get('Filesystem', 'folder_create_mask'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function writeTo($filename) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->makeDir(dirname($filename));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($filename) {
|
|
|
|
if(file_exists($filename)) list($width, $height, $type, $attr) = getimagesize($filename);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(file_exists($filename)) unlink($filename);
|
|
|
|
|
|
|
|
$ext = strtolower(substr($filename, strrpos($filename,'.')+1));
|
|
|
|
if(!isset($type)) switch($ext) {
|
2012-12-04 05:15:23 +01:00
|
|
|
case "gif": $type = IMAGETYPE_GIF; break;
|
|
|
|
case "jpeg": case "jpg": case "jpe": $type = IMAGETYPE_JPEG; break;
|
|
|
|
default: $type = IMAGETYPE_PNG; break;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-10-19 13:46:23 +02:00
|
|
|
|
|
|
|
// if $this->interlace != 0, the output image will be interlaced
|
|
|
|
imageinterlace ($this->gd, $this->interlace);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// if the extension does not exist, the file will not be created!
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
switch($type) {
|
2012-12-04 05:15:23 +01:00
|
|
|
case IMAGETYPE_GIF: imagegif($this->gd, $filename); break;
|
|
|
|
case IMAGETYPE_JPEG: imagejpeg($this->gd, $filename, $this->quality); break;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// case 3, and everything else
|
2014-08-15 08:53:05 +02:00
|
|
|
default:
|
2007-07-19 12:40:28 +02:00
|
|
|
// Save them as 8-bit images
|
|
|
|
// imagetruecolortopalette($this->gd, false, 256);
|
|
|
|
imagepng($this->gd, $filename); break;
|
|
|
|
}
|
|
|
|
if(file_exists($filename)) @chmod($filename,0664);
|
2014-01-02 16:59:34 +01:00
|
|
|
|
|
|
|
// Remove image manipulation from cache now that it's complete
|
|
|
|
$cacheData = unserialize($this->cache->load($this->cacheKey));
|
|
|
|
if(isset($cacheData[$this->manipulation])) unset($cacheData[$this->manipulation]);
|
|
|
|
$this->cache->save(serialize($cacheData), $this->cacheKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Image $frontend
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function onBeforeDelete($frontend) {
|
|
|
|
$file = Director::baseFolder() . "/" . $frontend->Filename;
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
$key = md5(implode('_', array($file, filemtime($file))));
|
|
|
|
$this->cache->remove($key);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-11-27 11:20:19 +01:00
|
|
|
|
|
|
|
/**
|
2013-11-29 05:12:47 +01:00
|
|
|
* This class is maintained for backwards-compatibility only. Please use the {@link GDBackend} class instead.
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage filesystem
|
2012-11-27 11:20:19 +01:00
|
|
|
*/
|
|
|
|
class GD extends GDBackend {
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use the "GDBackend.default_quality" config setting instead
|
2013-03-21 19:48:54 +01:00
|
|
|
*/
|
2012-11-27 11:20:19 +01:00
|
|
|
public static function set_default_quality($quality) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "GDBackend.default_quality" config setting instead');
|
2012-11-27 11:20:19 +01:00
|
|
|
GDBackend::set_default_quality($quality);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|