
Untitled
By: a guest on
Jun 22nd, 2012 | syntax:
None | size: 1.79 KB | hits: 14 | expires: Never
/**
* Resizes current image and returns self
*
* @param integer $width
* @param integer $height
*
* @throws InvalidArgumentException
* @throws RuntimeException
*
* @return Image_Image
*/
public function resize($width, $height, $mode = self::RESIZE_INSET)
{
if ($width < 1 || $height < 1) {
throw new InvalidArgumentException(
'Width and height of the resize must be positive integers'
);
}
try {
$size = $this->getSize();
switch ($mode) {
case self::RESIZE_INSET:
$ratio = min($width/$size['width'], $height/$size['height']);
break;
case self::RESIZE_OUTBOUND:
$ratio = max($width/$size['width'], $height/$size['height']);
break;
default:
throw new InvalidArgumentException(
'Invalid mode specified'
);
}
$resized = $this->copy();
foreach ($resized->imagick as $frame) {
$page = $frame->getImagePage();
$w = intval($frame->getImageWidth() * $ratio);
$h = intval($frame->getImageHeight() * $ratio);
$x = intval($page['x'] * $ratio);
$y = intval($page['y'] * $ratio);
$frame->resizeImage($w, $h, Imagick::FILTER_LANCZOS, 0);
$frame->setImagePage($w, $h, $x, $y);
}
} catch (ImagickException $e) {
throw new RuntimeException(
'Resize operation failed', $e->getCode()
);
}
return $resized;
}