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)); }
/** * Create a thumbnail of the source $file with given $transforms * @param string $file Source File (see $this->processFile()'s return value) * @param Transforms\AbstractTransform[] $transforms Transforms ro be applied * @return string|bool Thumb url or false on fail */ public function createThumb($file, $transforms = []) { $finfo = pathinfo($file); $relDir = $this->getFileDir($file); $thbPath = "/{$relDir}/" . $finfo['filename'] . Helper::serializeTransforms($transforms) . '.' . $finfo['extension']; if (is_dir($this->_baseThumb . "/{$relDir}")) { if (file_exists($this->_baseThumb . $thbPath)) { return $this->_webThumb . $thbPath; } } elseif (!mkdir($this->_baseThumb . "/{$relDir}", 0775, true)) { return false; } $imagine = $this->getImagine(); $image = $imagine->open($this->_baseSrc . "/{$relDir}/{$file}"); foreach ($transforms as $transform) { $transform->apply($image, $imagine); } $image->save($this->_baseThumb . $thbPath, ['quality' => 90]); return $this->_webThumb . $thbPath; }