2012-10-25 00:28:39 +02:00
|
|
|
<?php
|
2012-11-11 03:13:36 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetContainer;
|
|
|
|
use SilverStripe\Filesystem\Storage\AssetStore;
|
|
|
|
|
2012-11-11 03:13:36 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage filesystem
|
|
|
|
*/
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!class_exists('Imagick')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
class ImagickBackend extends Imagick implements Image_Backend {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-11 03:13:36 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2012-11-11 03:13:36 +01:00
|
|
|
* @var int
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_quality = 75;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Create a new backend with the given object
|
2012-10-25 00:28:39 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param AssetContainer $assetContainer Object to load from
|
2012-10-25 00:28:39 +02:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public function __construct(AssetContainer $assetContainer = null) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
if($assetContainer) {
|
|
|
|
$this->loadFromContainer($assetContainer);
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function loadFromContainer(AssetContainer $assetContainer) {
|
|
|
|
$stream = $assetContainer->getStream();
|
|
|
|
$this->readimagefile($stream);
|
|
|
|
fclose($stream);
|
|
|
|
$this->setDefaultQuality();
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function loadFrom($path) {
|
|
|
|
$this->readimage($path);
|
|
|
|
$this->setDefaultQuality();
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
protected function setDefaultQuality() {
|
|
|
|
$this->setQuality(Config::inst()->get('ImagickBackend', 'default_quality'));
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function writeToStore(AssetStore $assetStore, $filename, $hash = null, $variant = null, $config = array()) {
|
2015-09-15 04:52:02 +02:00
|
|
|
// Write to temporary file, taking care to maintain the extension
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'imagemagick');
|
|
|
|
if($extension = pathinfo($filename, PATHINFO_EXTENSION)) {
|
|
|
|
$path .= "." . $extension;
|
|
|
|
}
|
|
|
|
$this->writeimage($path);
|
2015-12-09 22:19:23 +01:00
|
|
|
$result = $assetStore->setFromLocalFile($path, $filename, $hash, $variant, $config);
|
2015-09-15 04:52:02 +02:00
|
|
|
unlink($path);
|
|
|
|
return $result;
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function writeTo($path) {
|
|
|
|
Filesystem::makeFolder(dirname($path));
|
|
|
|
if(is_dir(dirname($path))) {
|
|
|
|
$this->writeImage($path);
|
|
|
|
}
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setQuality($quality) {
|
|
|
|
$this->setImageCompressionQuality($quality);
|
2014-01-02 16:59:34 +01:00
|
|
|
}
|
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function resize($width, $height) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-10-30 00:30:51 +01:00
|
|
|
|
2015-09-15 04:52:02 +02: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");
|
|
|
|
}
|
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-10-25 00:28:39 +02:00
|
|
|
$geometry = $this->getImageGeometry();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
// Check that a resize is actually necessary.
|
2015-09-15 04:52:02 +02:00
|
|
|
if ($width === $geometry["width"] && $height === $geometry["height"]) {
|
2012-10-25 00:28:39 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = clone $this;
|
|
|
|
$new->resizeImage($width, $height, self::FILTER_LANCZOS, 1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
return $new;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function resizeRatio($maxWidth, $maxHeight, $useAsMinimum = false) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$geometry = $this->getImageGeometry();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$widthRatio = $maxWidth / $geometry["width"];
|
|
|
|
$heightRatio = $maxHeight / $geometry["height"];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
if( $widthRatio < $heightRatio ) {
|
|
|
|
return $useAsMinimum
|
|
|
|
? $this->resizeByHeight( $maxHeight )
|
|
|
|
: $this->resizeByWidth( $maxWidth );
|
|
|
|
} else {
|
|
|
|
return $useAsMinimum
|
|
|
|
? $this->resizeByWidth( $maxWidth )
|
|
|
|
: $this->resizeByHeight( $maxHeight );
|
|
|
|
}
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function resizeByWidth($width) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$geometry = $this->getImageGeometry();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$heightScale = $width / $geometry["width"];
|
|
|
|
return $this->resize( $width, $heightScale * $geometry["height"] );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function resizeByHeight($height) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$geometry = $this->getImageGeometry();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$scale = $height / $geometry["height"];
|
|
|
|
return $this->resize( $scale * $geometry["width"], $height );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-28 01:57:32 +02:00
|
|
|
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-07-28 02:19:34 +02:00
|
|
|
$new = $this->resizeRatio($width, $height);
|
|
|
|
$new->setImageBackgroundColor("#".$backgroundColor);
|
|
|
|
$w = $new->getImageWidth();
|
|
|
|
$h = $new->getImageHeight();
|
|
|
|
$new->extentImage($width,$height,($w-$width)/2,($h-$height)/2);
|
2012-10-25 00:28:39 +02:00
|
|
|
return $new;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
public function croppedResize($width, $height) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
$width = round($width);
|
|
|
|
$height = round($height);
|
2013-05-07 17:07:58 +02:00
|
|
|
$geo = $this->getImageGeometry();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-25 00:28:39 +02:00
|
|
|
// Check that a resize is actually necessary.
|
2013-05-07 17:07:58 +02:00
|
|
|
if ($width == $geo["width"] && $height == $geo["height"]) {
|
2012-10-25 00:28:39 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = clone $this;
|
2014-07-28 01:01:05 +02:00
|
|
|
$new->setBackgroundColor(new ImagickPixel('transparent'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-07 17:07:58 +02:00
|
|
|
if(($geo['width']/$width) < ($geo['height']/$height)){
|
2015-09-15 04:52:02 +02:00
|
|
|
$new->cropImage(
|
|
|
|
$geo['width'],
|
|
|
|
floor($height*$geo['width']/$width),
|
|
|
|
0,
|
|
|
|
($geo['height'] - ($height*$geo['width']/$width))/2
|
|
|
|
);
|
2013-05-07 17:07:58 +02:00
|
|
|
}else{
|
2015-09-15 04:52:02 +02:00
|
|
|
$new->cropImage(
|
|
|
|
ceil($width*$geo['height']/$height),
|
|
|
|
$geo['height'],
|
|
|
|
($geo['width'] - ($width*$geo['height']/$height))/2,
|
|
|
|
0
|
|
|
|
);
|
2012-10-25 00:28:39 +02:00
|
|
|
}
|
2013-05-07 17:07:58 +02:00
|
|
|
$new->ThumbnailImage($width,$height,true);
|
2012-10-25 00:28:39 +02:00
|
|
|
return $new;
|
|
|
|
}
|
2013-08-21 08:54:05 +02:00
|
|
|
}
|