/** * Generates a thumbnail of the image * * @param $dimension * * @return string */ public function generate($dimension) { list($width, $height) = Utils::getDimension($dimension); list($path, $basename, $extension) = $this->pathComponents(); $thumbnail = sprintf('%s/%s-%dx%d.%s', $path, $basename, $width, $height, $extension); Image::thumbnail($this->imagePath, $width, $height)->save($thumbnail); return $thumbnail; }
/** * Performs resize of the image. Imagine component is used. * * @param ImageInterface $imagine * @param $resize * @param bool $crop * * @return BoxInterface */ private function performResize($imagine, $resize, $crop = true) { $box = $imagine->getSize(); list($width, $height) = Utils::getDimension($resize); $box = $box->scale(max($width / $box->getWidth(), $height / $box->getHeight())); $imagine->resize($box); if ($crop) { $point = new Point(($box->getWidth() - $width) / 2, ($box->getHeight() - $height) / 2); $imagine->crop($point, new Box($width, $height)); } return $box; }
/** * @param BaseActiveRecord $owner * @param $attribute * @param $dimension * @param bool $absoluteUrl * * @return mixed|null|string */ public function thumbnail($owner, $attribute, $dimension, $absoluteUrl = true) { $value = $owner->getAttribute($attribute); if ((new UrlValidator())->validate($value)) { return $value; } if ($value && file_exists($value)) { return Url::to('@web/' . (new Thumbnailer(['imagePath' => $value]))->generate($dimension), $absoluteUrl); } if ($this->placeholder) { list($width, $height) = Utils::getDimension($dimension); return call_user_func([$this->placeholder, 'getImage'], $width, $height); } return null; }