SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/ResizeImageTransform.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16.  
  17. /**
  18. * Does image resizing, etc
  19. **/
  20. class ResizeImageTransform implements ImageTransform
  21. {
  22. private $width;
  23. private $height;
  24. private $master;
  25.  
  26.  
  27. /**
  28.   * Constructor
  29.   *
  30.   * @param int $width The width to resize the image to.
  31.   * @param int $height The height to resize the image to.
  32.   * @param int $master Optional master dimension. Image::WIDTH or Image::HEIGHT
  33.   **/
  34. public function __construct($width, $height, $master = null)
  35. {
  36. $this->width = $width;
  37. $this->height = $height;
  38. $this->master = $master;
  39. }
  40.  
  41.  
  42. /**
  43.   * Does the actual transform
  44.   *
  45.   * @param Image $img The image to transform
  46.   **/
  47. public function transform(Image $img)
  48. {
  49. if (
  50. ($this->width == null or $img->width < $this->width)
  51. and
  52. ($this->height == null or $img->height < $this->height)
  53. ) {
  54. return false;
  55. }
  56.  
  57. $img->resize($this->width, $this->height, $this->master);
  58. return true;
  59. }
  60.  
  61.  
  62. /**
  63.   * Estimate the RAM requirement to run this transform
  64.   *
  65.   * @return int Bytes
  66.   **/
  67. function estimateRamRequirement()
  68. {
  69. return $this->width * $this->height * 4;
  70. }
  71.  
  72.  
  73. /**
  74.   * Gets the dimensions
  75.   * @return array Keys are width, height, and master; these match the constructor args
  76.   */
  77. function getDimensions()
  78. {
  79. return ['width' => $this->width, 'height' => $this->height, 'master' => $this->master];
  80. }
  81.  
  82. }
  83.  
  84.  
  85.