SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/CropImageTransform.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 CropImageTransform implements ImageTransform
  21. {
  22. private $width;
  23. private $height;
  24. private $vert_pos;
  25. private $horiz_pos;
  26.  
  27.  
  28. /**
  29.   * Constructor
  30.   *
  31.   * @param int $width The width to crop the image at.
  32.   * @param int $height The height to crop the image at.
  33.   * @param mixed $vert_pos The position of the crop. A pixel value, or one of 'top', 'center' or 'bottom'.
  34.   * @param mixed $horiz_pos The position of the crop. A pixel value, or one of 'left', 'center' or 'right'.
  35.   **/
  36. public function __construct($width, $height, $vert_pos = 'center', $horiz_pos = 'center')
  37. {
  38. $this->width = $width;
  39. $this->height = $height;
  40. $this->vert_pos = $vert_pos;
  41. $this->horiz_pos = $horiz_pos;
  42. }
  43.  
  44.  
  45. /**
  46.   * Does the actual transform
  47.   *
  48.   * @param Image $img The image to transform
  49.   **/
  50. public function transform(Image $img)
  51. {
  52. $width_bad = $height_bad = false;
  53.  
  54. if ($this->width == null or $img->width < $this->width) $width_bad = true;
  55. if ($this->height == null or $img->height < $this->height) $height_bad = true;
  56.  
  57. if ($width_bad and $height_bad) return false;
  58.  
  59. $img->crop($this->width, $this->height, $this->vert_pos, $this->horiz_pos);
  60. return true;
  61. }
  62.  
  63.  
  64. /**
  65.   * Estimate the RAM requirement to run this transform
  66.   *
  67.   * @return int Bytes
  68.   **/
  69. function estimateRamRequirement()
  70. {
  71. return $this->width * $this->height * 4;
  72. }
  73.  
  74. }
  75.  
  76.  
  77.