protected function _resize($width, $height = null, $keepar = true, $pad = true)
{
$sizes = $this->sizes();
if ($height == null or $width == null) {
if ($height == null and substr($width, -1) == '%') {
$height = $width;
} elseif (substr($height, -1) == '%' and $width == null) {
$width = $height;
} else {
if ($height == null and $width != null) {
$height = $width * ($sizes->height / $sizes->width);
} elseif ($height != null and $width == null) {
$width = $height * ($sizes->width / $sizes->height);
} else {
throw new \InvalidArgumentException("Width and height cannot be null.");
}
}
}
$origwidth = $this->convert_number($width, true);
$origheight = $this->convert_number($height, false);
$width = $origwidth;
$height = $origheight;
$x = 0;
$y = 0;
if ($keepar) {
// See which is the biggest ratio
if (function_exists('bcdiv')) {
$width_ratio = bcdiv((double) $width, $sizes->width, 10);
$height_ratio = bcdiv((double) $height, $sizes->height, 10);
$compare = bccomp($width_ratio, $height_ratio, 10);
if ($compare > -1) {
$height = ceil((double) bcmul($sizes->height, $height_ratio, 10));
$width = ceil((double) bcmul($sizes->width, $height_ratio, 10));
} else {
$height = ceil((double) bcmul($sizes->height, $width_ratio, 10));
$width = ceil((double) bcmul($sizes->width, $width_ratio, 10));
}
} else {
$width_ratio = $width / $sizes->width;
$height_ratio = $height / $sizes->height;
if ($width_ratio >= $height_ratio) {
$height = ceil($sizes->height * $height_ratio);
$width = ceil($sizes->width * $height_ratio);
} else {
$height = ceil($sizes->height * $width_ratio);
$width = ceil($sizes->width * $width_ratio);
}
}
}
if ($pad) {
$x = floor(($origwidth - $width) / 2);
$y = floor(($origheight - $height) / 2);
} else {
$origwidth = $width;
$origheight = $height;
}
//it's more efficient to save it to the temp file, and do our changes there
//$this->imagick()->setImageGravity(\Imagick::GRAVITY_CENTER);
//$this->imagick()->resizeImage($width, $height);
$image = '"' . $this->image() . '"';
$this->exec('convert', "-define png:size=" . $origwidth . "x" . $origheight . " " . $image . " " . "-background none " . "-resize \"" . ($pad ? $width : $origwidth) . "x" . ($pad ? $height : $origheight) . "!\" " . "-gravity center " . "-extent " . $origwidth . "x" . $origheight . " " . $image);
}