Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/Imagine/GD/Command/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ public function __construct($width, $height, $mode = null)
*/
public function process(\Imagine\Image $image)
{
if ($this->mode) {
$this->adjustSize($image);
}
list($width, $height) = $this->adjustSize($image);

$srcImage = $image->getResource();
$dstImage = Utils::createResource($this->width, $this->height, $image->getType());
$dstImage = Utils::createResource($width, $height, $image->getType());

if (! imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $this->width, $this->height, $image->getWidth(), $image->getHeight())) {
if (! imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight())) {
throw new \RuntimeException('Could not resize the image');
}
$image->setResource($dstImage);
Expand All @@ -94,25 +92,35 @@ public function process(\Imagine\Image $image)
* from the other dimension and the image's aspect ratio.
*
* @param \Imagine\Image $image
* @return array width, height
*/
private function adjustSize(\Imagine\Image $image)
{
switch ($this->mode) {
case self::INFER_HEIGHT:
$this->height = intval($this->width * $image->getHeight() / $image->getWidth());
$width = $this->width;
$height = intval($this->width * $image->getHeight() / $image->getWidth());
break;

case self::INFER_WIDTH:
$this->width = intval($this->height * $image->getWidth() / $image->getHeight());
$width = intval($this->height * $image->getWidth() / $image->getHeight());
$height = $this->height;
break;

case self::AR_AROUND:
list($this->width, $this->height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, true);
list($width, $height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, true);
break;

case self::AR_WITHIN:
list($this->width, $this->height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, false);
list($width, $height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, false);
break;

default:
$width = $this->width;
$height = $this->height;
break;
}

return array($width, $height);
}
}