Source of file ResizedImage.php
Size: 7,253 Bytes - Last Modified: 2020-01-07T10:44:43+00:00
/var/www/docs.ssmods.com/process/src/code/views/ResizedImage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 | <?php class ResizedImage extends ViewableData { /** * @var int */ private $height; /** * @var int */ private $width; /** * @var float */ private $percentageWidth; /** * @var int */ private $image; /** * @var int */ private $marginRight; /** * @var float */ private $percentageMarginRight; /** * @var int */ private $marginTop; /** * @var float */ private $percentageMarginTop; /** * @var Image */ private $previewImage; /** * ResizedImage constructor. * @param Image $image */ public function __construct(Image $image) { parent::__construct(); $this->image = $image; $this->height = $image->getHeight(); $this->width = $image->getWidth(); $this->marginTop = $this->getConfiguredMargin(); $this->marginRight = $this->getConfiguredMargin(); } /** * Returns the configured margin of all images. * @return int */ public static function getConfiguredMargin() { return (int) self::config()->get('margin'); } /** * Returns the configured max preview height of all images. * @return int */ public static function getMaxPreviewHeight() { return (int) self::config()->get('max_preview_height'); } /** * Returns the configured preview padding of all images. * @return int */ public static function getPreviewPadding() { return (int) self::config()->get('preview_padding'); } /** * Returns the height of this resized image: * @see resize() * @return int */ public function getHeight() { return $this->height; } /** * Returns the width of this resized image: * @see resize() * @return int */ public function getWidth() { return $this->width; } /** * Set the height of the image by hard. This function does not keep the aspect ratio. * Attention: This is not the height of real image, which is stored in $this->image. * This is just the height which is applied to the real image when calling resize() on this object. * @param int $height */ public function setHeight($height) { $this->height = $height; } /** * Set the width of the image by hard. This function does not keep the aspect ratio. * Attention: This is not the width of real image, which is stored in $this->image. * This is just the width which is applied to the real image when calling resize() on this object. * @param int $width */ public function setWidth($width) { $this->width = $width; } /** * Sets the width of this image in percent of the width of the overlying line. * @param float $percentageWidth */ public function setPercentageWidth($percentageWidth) { if ($percentageWidth < 0) { throw new InvalidArgumentException('The percentage width can not be smaller than 0.'); } $this->percentageWidth = $percentageWidth; } /** * Returns the width in percent of the overlying line. * @return float */ public function getPercentageWidth() { return $this->percentageWidth; } /** * Add width to image. This function does not keep the aspect ratio. * @param int $width */ public function addWidth($width) { $this->width += $width; } /** * Returns the margin at the right side of this image. * @return int */ public function getMarginRight() { return $this->marginRight; } /** * Returns the right margin in percent of the overlying line. * @return float */ public function getPercentageMarginRight() { return $this->percentageMarginRight; } /** * Set the margin at the right side of this image. * @param int $marginRight */ public function setMarginRight($marginRight) { $this->marginRight = $marginRight; } /** * Returns the margin at the top of this image. * @return int */ public function getMarginTop() { return $this->marginTop; } /** * Returns the top margin in percentage of the overlying line. * @return float */ public function getPercentageMarginTop() { return $this->percentageMarginTop; } /** * * Set the margin at the top of this image. * @param int $marginTop */ public function setMarginTop($marginTop) { $this->marginTop = $marginTop; } /** * @param float $percentageMarginRight */ public function setPercentageMarginRight($percentageMarginRight) { if ($percentageMarginRight < 0) { throw new InvalidArgumentException('The right margin in percent can not be smaller than 0.'); } $this->percentageMarginRight = $percentageMarginRight; } /** * @param float $percentageMarginTop */ public function setPercentageMarginTop($percentageMarginTop) { if ($this->percentageMarginTop < 0) { throw new InvalidArgumentException('The top margin in percent can not be smaller than 0.'); } $this->percentageMarginTop = $percentageMarginTop; } /** * Returns the original image object. * This image will not be resized until resize() was called on this ResizedImage object. * Any modifications made to this object, by the function of this class are only applied, if resize() was called. * @see resize() * @return Image */ public function getImage() { return $this->image; } /** * Returns the preview image. * @return Image */ public function getPreviewImage() { return $this->previewImage; } /** * Sets the preview image. The preview image should be a larger version of the resized image. * This image is shown when the user clicks on an image. * @param Image $previewImage */ public function setPreviewImage($previewImage) { $this->previewImage = $previewImage; } /** * Change height by keeping aspect ratio. * @param int $height */ public function changeHeight($height) { $factor = $height / $this->height; $this->height = $height; $this->width = $this->width * $factor; $this->correctIllegalSizes(); } /** * Change width by keeping aspect ration. * @param int $width */ public function changeWidth($width) { $factor = $width / $this->width; $this->width = $width; $this->height = $this->height * $factor; $this->correctIllegalSizes(); } /** * This function resizes the image behind this object, which is stored in $this->image. * Any modifications made to this object, by the function of this class are only applied, if resize() was called. */ public function resize() { $this->image->Pad($this->getWidth(), $this->getHeight(), 'FFFFFF', 100); } /** * Set sizes smaller than 1 to 1, because a size of 0 is not allowed. */ private function correctIllegalSizes() { if ($this->width < 1) $this->width = 1; if ($this->height < 1) $this->height = 1; } /** * This function is called when this object is rendered to a template. * @return HTMLText */ public function forTemplate() { return $this->renderWith('ResizedImage'); } } |