Source of file GalleryImage.php
Size: 7,169 Bytes - Last Modified: 2021-12-23T10:09:48+00:00
/var/www/docs.ssmods.com/process/src/src/Views/GalleryImage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | <?php namespace PaulSchulz\SilverStripe\Gallery\Views; use InvalidArgumentException; use PaulSchulz\SilverStripe\Gallery\Exceptions\IllegalStateException; use PaulSchulz\SilverStripe\Gallery\Exceptions\InvalidConfigurationException; use SilverStripe\Assets\Image; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\View\ViewableData; /** * This class represents an image of an image collection or a gallery. * All operations done on this object, like setScaleByWidth(), are not applied to the underlying image. * Instead $this->scale is changed, to just calculate the dimensions of this image for performance reasons. * * @package PaulSchulz\SilverStripe\GalleryExtension\Views */ class GalleryImage extends ViewableData { /** * @var Image $image */ protected $image; /** * The scale factor of this image. * @var float $scale */ protected $scale = 1; /** * The width of the image line this image is wrapped in. * This property is only set, when this object is rendered to a template, so when forTemplate() of the ImageLine class is called. * @var int $lineWidth */ protected $lineWidth = 0; /** * Determines if this image is rendered with margin at the top or not. * This property is only set, when this object is rendered to a template, so when forTemplate() of the ImageLine class is called. * @var bool $hasMarginTop */ protected $hasMarginTop = true; /** * Determines if this image is rendered with margin at the right or not. * This property is only set, when this object is rendered to a template, so when forTemplate() of the ImageLine class is called. * @var bool $hasMarginRight */ protected $hasMarginRight = true; /** * GalleryImage constructor. * @param Image $image */ public function __construct(Image $image) { parent::__construct(); $this->image = $image; } /** * Returns the underlying image. * @return Image */ public function getImage(): Image { return $this->image; } /** * Returns the margin of an image. The margin is applied on the top and at the right of the image. * This margin can be set in config.yml. * @throws InvalidConfigurationException * @return int */ public static function getMargin() : int { $margin = self::config()->get('margin'); if ($margin < 0) { throw new InvalidConfigurationException('A negative margin is no allowed.'); } if ((int) $margin != $margin) { throw new InvalidConfigurationException('Decimals as a value for the optimized width are not allowed.'); } return $margin; } /** * Sets the line width for the * @param $lineWidth */ public function setLineWidth($lineWidth) { if ($lineWidth <= 0) { throw new InvalidArgumentException('The $lineWidth must be greater or equal to 0.'); } $this->lineWidth = $lineWidth; } /** * Sets the scale by the height of the image. * This function does not touch the real image. It just sets $this->scale, for calculation reasons. * @param float $height */ public function setScaleByHeight(float $height) { if ($height < 0) { throw new InvalidArgumentException('A negative image height is not allowed.'); } $this->scale *= $height / ($this->image->getHeight() * $this->scale); } /** * Sets the scale by the width of the image. * This function does not touch the real image. It just sets $this->scale, for calculation reasons. * @param float $width */ public function setScaleByWidth(float $width) { if ($width < 0) { throw new InvalidArgumentException('A negative image width is not allowed.'); } $this->scale *= $width / ($this->image->getWidth() * $this->scale); } /** * Sets the value of $this->addMarginTop. * This is necessary to determine if the margin should be added to the top of the image in the template. * @param bool $hasMarginTop */ public function setHasMarginTop(bool $hasMarginTop) { $this->hasMarginTop = $hasMarginTop; } /** * Sets the value of $this->addMarginRight. * This is necessary to determine if the margin should be added to the right of the image in the template. * @param bool $hasMarginRight */ public function setHasMarginRight(bool $hasMarginRight) { $this->hasMarginRight = $hasMarginRight; } /** * Returns the value $this->addMarginTop. * This is necessary to determine if the margin should be added to the top of the image in the template. * @return bool */ public function HasMarginTop(): bool { return $this->hasMarginTop; } /** * Sets the value of $this->addMarginRight. * This is necessary to determine if the margin should be added to the right of the image in the template. * @return bool */ public function HasMarginRight(): bool { return $this->hasMarginRight; } /** * Scales this image dimensions about the factor $scale. * @param float $scale */ public function scale(float $scale) { if ($scale < 0) { throw new InvalidArgumentException('A negative scale factor is not allowed'); } $this->scale *= $scale; } /** * Returns the height of the image based on $this->scale. * @return float */ public function getScaledHeight() : float { return $this->image->getHeight() * $this->scale; } /** * Returns the height of the image based on $this->scale. * @return float */ public function getScaledWidth() : float { return $this->image->getWidth() * $this->scale; } /** * This function returns the value in $size as percentage of $this->lineWidth. * This is just a helper function for other function in this class. * @param $size * @return float */ protected function getPercentageOfLineWidth($size) : float { if ($this->lineWidth === 0) { throw new IllegalStateException('The fields $this->lineWidth must not be 0, when calling this function.'); } return $size / $this->lineWidth * 100; } /** * Returns the width in percent of the wrapping line. * This property is only set when a line is rendered to a template. * @return float */ public function getPercentageWidth() : float { return $this->getPercentageOfLineWidth($this->getScaledWidth()); } /** * Returns the margin of this image in percent of the width of one image line. * @throws InvalidConfigurationException * @return float */ public function getPercentageMargin() : float { return $this->getPercentageOfLineWidth(static::getMargin()); } /** * This function is called when this object should be rendered to a template. * @return DBHTMLText */ public function forTemplate() { return $this->renderWith(self::class); } } |