PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/js/tiny_mce/plugins/kcfinder/lib/class_image.php

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