Source of file ProductImageExtension.php
Size: 2,731 Bytes - Last Modified: 2021-12-24T06:35:03+00:00
/var/www/docs.ssmods.com/process/src/src/Extension/ProductImageExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 | <?php namespace SilverShop\Extension; use SilverStripe\Assets\Image; use SilverStripe\Core\Config\Configurable; use SilverStripe\ORM\DataExtension; /** * Adds some image size functions to the Image DataObject. * * @package shop */ class ProductImageExtension extends DataExtension { use Configurable; /** * @var Image */ protected $owner; /** * @param bool $upscale [optional] * @return Image */ public function getThumbnail($upscale = false) { $width = self::config()->thumbnail_width; $height = self::config()->thumbnail_height; return $this->getImageAt($width, $height, $upscale); } /** * @param bool $upscale [optional] * @return Image */ public function getContentImage($upscale = false) { $width = self::config()->content_image_width; $height = self::config()->content_image_height; return $this->getImageAt($width, $height, $upscale); } /** * @param bool $upscale [optional] * @return Image */ public function getLargeImage($upscale = false) { $width = self::config()->large_image_width; $height = self::config()->large_image_height; return $this->getImageAt($width, $height, $upscale); } /** * Resizes image by width or height only if the source image is bigger than the given width/height. * This prevents ugly upscaling. * * @param int $width [optional] * @param int $height [optional] * @param bool $upscale [optional] * * @return Image */ public function getImageAt($width = null, $height = null, $upscale = false) { if (!$this->owner->exists()) { return $this->owner; } $realWidth = $this->owner->getWidth(); $realHeight = $this->owner->getHeight(); if ($width && $height) { return $realWidth < $width && $realHeight < $height && !$upscale ? $this->owner : $this->owner->Pad($width, $height); } else { if ($width) { return $realWidth < $width && !$upscale ? $this->owner : $this->owner->ScaleWidth($width); } else { return $realHeight < $height && !$upscale ? $this->owner : $this->owner->ScaleHeight($height); } } } /** * @return bool - is the image large enough that a "large" image makes sense? */ public function HasLargeImage() { $imageWidth = intval($this->owner->getWidth()); return $imageWidth > self::config()->content_image_width; } } |