Skip to content

Commit

Permalink
Merge pull request silverstripe#3334 from jedateach/fixes/imagick-pad…
Browse files Browse the repository at this point in the history
…resize

Image Magick PaddedResize fix
  • Loading branch information
halkyon committed Aug 19, 2014
2 parents ef272be + 02265dc commit 8bd867e
Showing 1 changed file with 7 additions and 42 deletions.
49 changes: 7 additions & 42 deletions filesystem/ImagickBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ class ImagickBackend extends Imagick implements Image_Backend {
public function __construct($filename = null) {
if(is_string($filename)) {
parent::__construct($filename);
} else {
self::setImageCompressionQuality($this->config()->default_quality);
}
$this->setQuality(Config::inst()->get('ImagickBackend','default_quality'));
}

/**
Expand Down Expand Up @@ -184,46 +183,12 @@ public function resizeByHeight($height) {
* @param int $height
* @return Image_Backend
*/
public function paddedResize($width, $height, $backgroundColor = "#FFFFFF00") {
if(!$this->valid()) return;

$width = round($width);
$height = round($height);
$geometry = $this->getImageGeometry();

// Check that a resize is actually necessary.
if ($width == $geometry["width"] && $height == $geometry["height"]) {
return $this;
}

$new = clone $this;
$new->setBackgroundColor($backgroundColor);

$destAR = $width / $height;
if ($geometry["width"] > 0 && $geometry["height"] > 0) {
// We can't divide by zero theres something wrong.

$srcAR = $geometry["width"] / $geometry["height"];

// Destination narrower than the source
if($destAR > $srcAR) {
$destY = 0;
$destHeight = $height;

$destWidth = round( $height * $srcAR );
$destX = round( ($width - $destWidth) / 2 );

// Destination shorter than the source
} else {
$destX = 0;
$destWidth = $width;

$destHeight = round( $width / $srcAR );
$destY = round( ($height - $destHeight) / 2 );
}

$new->extentImage($width, $height, $destX, $destY);
}
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
$new = $this->resizeRatio($width, $height);
$new->setImageBackgroundColor("#".$backgroundColor);
$w = $new->getImageWidth();
$h = $new->getImageHeight();
$new->extentImage($width,$height,($w-$width)/2,($h-$height)/2);

return $new;
}
Expand Down

0 comments on commit 8bd867e

Please sign in to comment.