public function apply(ImageInterface &$image, ImagineInterface $imagine) { $box = $image->getSize(); $width = Helper::percentValue($this->_width, $box->getWidth()); $height = Helper::percentValue($this->_height, $box->getHeight()); // no upscale if ($box->getWidth() <= $width && $box->getHeight() <= $height) { return; } Helper::scaleSize($width, $height, $box); $image->resize(new Box($width, $height)); }
public function apply(ImageInterface &$image, ImagineInterface $imagine) { $box = $image->getSize(); // x and y is the center of the crop $x = Helper::percentValue($this->_x, $boxw = $box->getWidth()); $y = Helper::percentValue($this->_y, $boxh = $box->getHeight()); $w = Helper::percentValue($this->_width, $boxw); $h = Helper::percentValue($this->_height, $boxh); if ($this->_ratio) { switch ($this->_ratio) { case self::COVER: Helper::scaleSize($w, $h, $box); $w = $h = min($w, $h); break; case self::CONTAIN: Helper::scaleSize($w, $h, $box); $max = max($w, $h); $img = $imagine->create(new Box($max, $max), new Color(0, 100)); $img->paste($image, new Point(($max - $boxw) * 0.5, ($max - $boxh) * 0.5)); $image = $img; return; default: // custom ratio $this->fitByRatio($w, $h, $w && $h ? $w / $h : 0); if (!$w || !$h) { throw new \RuntimeException('Invalid ratio supplied'); } break; } } else { Helper::scaleSize($w, $h, $box); } $halfw = $w / 2; $halfh = $h / 2; if ($x + $halfw > $boxw) { $x = $boxw - $halfw; } if ($y + $halfh > $boxh) { $y = $boxh - $halfh; } if ($x < $halfw) { $x = $halfw; } if ($y < $halfh) { $y = $halfh; } $image->crop(new Point($x - $w / 2, $y - $h / 2), new Box($w, $h)); }