class_image.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc Abstract image driver class
  5. * @package KCFinder
  6. * @version 3.12
  7. * @author Pavel Tzonkov <sunhater@sunhater.com>
  8. * @copyright 2010-2014 KCFinder Project
  9. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  10. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. namespace kcfinder;
  14. abstract class image {
  15. const DEFAULT_JPEG_QUALITY = 75;
  16. /** Image resource or object
  17. * @var mixed */
  18. protected $image;
  19. /** Image width in pixels
  20. * @var integer */
  21. protected $width;
  22. /** Image height in pixels
  23. * @var integer */
  24. protected $height;
  25. /** Init error
  26. * @var bool */
  27. protected $initError = false;
  28. /** Driver specific options
  29. * @var array */
  30. protected $options = array();
  31. /** Magic method which allows read-only access to all protected or private
  32. * class properties
  33. * @param string $property
  34. * @return mixed */
  35. final public function __get($property) {
  36. return property_exists($this, $property) ? $this->$property : null;
  37. }
  38. /** Constructor. Parameter $image should be:
  39. * 1. An instance of image driver class (copy instance).
  40. * 2. An image represented by the type of the $image property
  41. * (resource or object).
  42. * 3. An array with two elements. First - width, second - height.
  43. * Creates a blank image.
  44. * 4. A filename string. Get image form file.
  45. * Second paramaeter is used by pass some specific image driver options
  46. * @param mixed $image
  47. * @param array $options */
  48. public function __construct($image, array $options=array()) {
  49. $this->image = $this->width = $this->height = null;
  50. $imageDetails = $this->buildImage($image);
  51. if ($imageDetails !== false)
  52. list($this->image, $this->width, $this->height) = $imageDetails;
  53. else
  54. $this->initError = true;
  55. $this->options = $options;
  56. }
  57. /** Factory pattern to load selected driver. $image and $options are passed
  58. * to the constructor of the image driver
  59. * @param string $driver
  60. * @param mixed $image
  61. * @return object */
  62. final static function factory($driver, $image, array $options=array()) {
  63. $class = __NAMESPACE__ . "\\image_$driver";
  64. return new $class($image, $options);
  65. }
  66. /** Checks if the drivers in the array parameter could be used. Returns first
  67. * found one
  68. * @param array $drivers
  69. * @return string */
  70. final static function getDriver(array $drivers=array('gd')) {
  71. foreach ($drivers as $driver) {
  72. if (!preg_match('/^[a-z0-9\_]+$/i', $driver))
  73. continue;
  74. $class = __NAMESPACE__ . "\\image_$driver";
  75. if (class_exists($class) && method_exists($class, "available")) {
  76. eval("\$avail = $class::available();");
  77. if ($avail) return $driver;
  78. }
  79. }
  80. return false;
  81. }
  82. /** Returns an array. Element 0 - image resource. Element 1 - width. Element 2 - height.
  83. * Returns FALSE on failure.
  84. * @param mixed $image
  85. * @return array */
  86. final protected function buildImage($image) {
  87. $class = get_class($this);
  88. if ($image instanceof $class) {
  89. $width = $image->width;
  90. $height = $image->height;
  91. $img = $image->image;
  92. } elseif (is_array($image)) {
  93. list($key, $width) = each($image);
  94. list($key, $height) = each($image);
  95. $img = $this->getBlankImage($width, $height);
  96. } else
  97. $img = $this->getImage($image, $width, $height);
  98. return ($img !== false)
  99. ? array($img, $width, $height)
  100. : false;
  101. }
  102. /** Returns calculated proportional width from the given height
  103. * @param integer $resizedHeight
  104. * @return integer */
  105. final public function getPropWidth($resizedHeight) {
  106. $width = round(($this->width * $resizedHeight) / $this->height);
  107. if (!$width) $width = 1;
  108. return $width;
  109. }
  110. /** Returns calculated proportional height from the given width
  111. * @param integer $resizedWidth
  112. * @return integer */
  113. final public function getPropHeight($resizedWidth) {
  114. $height = round(($this->height * $resizedWidth) / $this->width);
  115. if (!$height) $height = 1;
  116. return $height;
  117. }
  118. /** Checks if PHP needs some extra extensions to use the image driver. This
  119. * static method should be implemented into driver classes like abstract
  120. * methods
  121. * @return bool */
  122. static function available() { return false; }
  123. /** Checks if file is an image. This static method should be implemented into
  124. * driver classes like abstract methods
  125. * @param string $file
  126. * @return bool */
  127. static function checkImage($file) { return false; }
  128. /** Resize image. Should return TRUE on success or FALSE on failure
  129. * @param integer $width
  130. * @param integer $height
  131. * @return bool */
  132. abstract public function resize($width, $height);
  133. /** Resize image to fit in given resolution. Should returns TRUE on success
  134. * or FALSE on failure. If $background is set, the image size will be
  135. * $width x $height and the empty spaces (if any) will be filled with defined
  136. * color. Background color examples: "#5f5", "#ff67ca", array(255, 255, 255)
  137. * @param integer $width
  138. * @param integer $height
  139. * @param mixed $background
  140. * @return bool */
  141. abstract public function resizeFit($width, $height, $background=false);
  142. /** Resize and crop the image to fit in given resolution. Returns TRUE on
  143. * success or FALSE on failure
  144. * @param mixed $src
  145. * @param integer $offset
  146. * @return bool */
  147. abstract public function resizeCrop($width, $height, $offset=false);
  148. /** Rotate image
  149. * @param integer $angle
  150. * @param string $background
  151. * @return bool */
  152. abstract public function rotate($angle, $background="#000000");
  153. abstract public function flipHorizontal();
  154. abstract public function flipVertical();
  155. /** Apply a PNG or GIF watermark to the image. $top and $left parameters sets
  156. * the offset of the watermark in pixels. Boolean and NULL values are possible
  157. * too. In default case (FALSE, FALSE) the watermark should be applyed to
  158. * the bottom right corner. NULL values means center aligning. If the
  159. * watermark is bigger than the image or it's partialy or fully outside the
  160. * image, it shoudn't be applied
  161. * @param string $file
  162. * @param mixed $top
  163. * @param mixed $left
  164. * @return bool */
  165. abstract public function watermark($file, $left=false, $top=false);
  166. /** Should output the image. Second parameter is used to pass some options like
  167. * 'file' - if is set, the output will be written to a file
  168. * 'quality' - compression quality
  169. * It's possible to use extra specific options required by image type ($type)
  170. * @param string $type
  171. * @param array $options
  172. * @return bool */
  173. abstract public function output($type='jpeg', array $options=array());
  174. /** This method should create a blank image with selected size. Should returns
  175. * resource or object related to the created image, which will be passed to
  176. * $image property
  177. * @param integer $width
  178. * @param integer $height
  179. * @return mixed */
  180. abstract protected function getBlankImage($width, $height);
  181. /** This method should create an image from source image. Only first parameter
  182. * ($image) is input. Its type should be filename string or a type of the
  183. * $image property. See the constructor reference for details. The
  184. * parametters $width and $height are output only. Should returns resource or
  185. * object related to the created image, which will be passed to $image
  186. * property
  187. * @param mixed $image
  188. * @param integer $width
  189. * @param integer $height
  190. * @return mixed */
  191. abstract protected function getImage($image, &$width, &$height);
  192. }
  193. ?>