SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Drivers/ImageDriver.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  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.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers\Drivers;
  17.  
  18.  
  19. /**
  20.  * Image API driver.
  21.  */
  22. abstract class ImageDriver {
  23.  
  24. // Reference to the current image
  25. protected $image;
  26.  
  27. // Reference to the temporary processing image
  28. protected $tmp_image;
  29.  
  30. // Processing errors
  31. protected $errors = array();
  32.  
  33. /**
  34.   * Executes a set of actions, defined in pairs.
  35.   *
  36.   * @param array actions
  37.   * @return boolean
  38.   */
  39. public function execute($actions)
  40. {
  41. foreach ($actions as $func => $args)
  42. {
  43. if ( ! $this->$func($args))
  44. return FALSE;
  45. }
  46.  
  47. return TRUE;
  48. }
  49.  
  50. /**
  51.   * Sanitize and normalize a geometry array based on the temporary image
  52.   * width and height. Valid properties are: width, height, top, left.
  53.   *
  54.   * @param array geometry properties
  55.   * @return void
  56.   */
  57. protected function sanitizeGeometry( & $geometry)
  58. {
  59. list($width, $height) = $this->properties();
  60.  
  61. // Turn off error reporting
  62. $reporting = error_reporting(0);
  63.  
  64. // Width and height cannot exceed current image size
  65. $geometry['width'] = min($geometry['width'], $width);
  66. $geometry['height'] = min($geometry['height'], $height);
  67.  
  68. // Set standard coordinates if given, otherwise use pixel values
  69. if ($geometry['top'] === 'center')
  70. {
  71. $geometry['top'] = floor(($height / 2) - ($geometry['height'] / 2));
  72. }
  73. elseif ($geometry['top'] === 'top')
  74. {
  75. $geometry['top'] = 0;
  76. }
  77. elseif ($geometry['top'] === 'bottom')
  78. {
  79. $geometry['top'] = $height - $geometry['height'];
  80. }
  81.  
  82. // Set standard coordinates if given, otherwise use pixel values
  83. if ($geometry['left'] === 'center')
  84. {
  85. $geometry['left'] = floor(($width / 2) - ($geometry['width'] / 2));
  86. }
  87. elseif ($geometry['left'] === 'left')
  88. {
  89. $geometry['left'] = 0;
  90. }
  91. elseif ($geometry['left'] === 'right')
  92. {
  93. $geometry['left'] = $width - $geometry['width'];
  94. }
  95.  
  96. // Restore error reporting
  97. error_reporting($reporting);
  98. }
  99.  
  100. /**
  101.   * Return the current width and height of the temporary image. This is mainly
  102.   * needed for sanitizing the geometry.
  103.   *
  104.   * @return array width, height
  105.   */
  106. abstract protected function properties();
  107.  
  108. /**
  109.   * Process an image with a set of actions.
  110.   *
  111.   * @param string image filename
  112.   * @param array actions to execute
  113.   * @param string destination directory path
  114.   * @param string destination filename
  115.   * @return boolean
  116.   */
  117. abstract public function process($image, $actions, $dir, $file);
  118.  
  119. /**
  120.   * Flip an image. Valid directions are horizontal and vertical.
  121.   *
  122.   * @param integer direction to flip
  123.   * @return boolean
  124.   */
  125. abstract function flip($direction);
  126.  
  127. /**
  128.   * Crop an image. Valid properties are: width, height, top, left.
  129.   *
  130.   * @param array new properties
  131.   * @return boolean
  132.   */
  133. abstract function crop($properties);
  134.  
  135. /**
  136.   * Resize an image. Valid properties are: width, height, and master.
  137.   *
  138.   * @param array new properties
  139.   * @return boolean
  140.   */
  141. abstract public function resize($properties);
  142.  
  143. /**
  144.   * Rotate an image. Valid amounts are -180 to 180.
  145.   *
  146.   * @param integer amount to rotate
  147.   * @return boolean
  148.   */
  149. abstract public function rotate($amount);
  150.  
  151. /**
  152.   * Sharpen and image. Valid amounts are 1 to 100.
  153.   *
  154.   * @param integer amount to sharpen
  155.   * @return boolean
  156.   */
  157. abstract public function sharpen($amount);
  158.  
  159. } // End Image Driver
  160.