PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ilya.webcity/anna
PHP | 305 lines | 241 code | 47 blank | 17 comment | 31 complexity | ca2ebe5c99cf0c5af1f6a029213c2c8f MD5 | raw file
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc ImageMagick 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. class image_imagick extends image {
  14. static $MIMES = array(
  15. //'tif' => "image/tiff"
  16. );
  17. // ABSTRACT PUBLIC METHODS
  18. public function resize($width, $height) {//
  19. if (!$width) $width = 1;
  20. if (!$height) $height = 1;
  21. try {
  22. $this->image->scaleImage($width, $height);
  23. } catch (Exception $e) {
  24. return false;
  25. }
  26. $this->width = $width;
  27. $this->height = $height;
  28. return true;
  29. }
  30. public function resizeFit($width, $height, $background=false) {//
  31. if (!$width) $width = 1;
  32. if (!$height) $height = 1;
  33. try {
  34. $this->image->scaleImage($width, $height, true);
  35. $size = $this->image->getImageGeometry();
  36. } catch (Exception $e) {
  37. return false;
  38. }
  39. if ($background === false) {
  40. $this->width = $size['width'];
  41. $this->height = $size['height'];
  42. return true;
  43. } else {
  44. try {
  45. $this->image->setImageBackgroundColor($background);
  46. $x = -round(($width - $size['width']) / 2);
  47. $y = -round(($height - $size['height']) / 2);
  48. $this->image->extentImage($width, $height, $x, $y);
  49. } catch (Exception $e) {
  50. return false;
  51. }
  52. $this->width = $width;
  53. $this->height = $height;
  54. return true;
  55. }
  56. }
  57. public function resizeCrop($width, $height, $offset=false) {
  58. if (!$width) $width = 1;
  59. if (!$height) $height = 1;
  60. if (($this->width / $this->height) > ($width / $height)) {
  61. $h = $height;
  62. $w = ($this->width * $h) / $this->height;
  63. $y = 0;
  64. if ($offset !== false) {
  65. if ($offset > 0)
  66. $offset = -$offset;
  67. if (($w + $offset) <= $width)
  68. $offset = $width - $w;
  69. $x = $offset;
  70. } else
  71. $x = ($width - $w) / 2;
  72. } else {
  73. $w = $width;
  74. $h = ($this->height * $w) / $this->width;
  75. $x = 0;
  76. if ($offset !== false) {
  77. if ($offset > 0)
  78. $offset = -$offset;
  79. if (($h + $offset) <= $height)
  80. $offset = $height - $h;
  81. $y = $offset;
  82. } else
  83. $y = ($height - $h) / 2;
  84. }
  85. $x = round($x);
  86. $y = round($y);
  87. $w = round($w);
  88. $h = round($h);
  89. if (!$w) $w = 1;
  90. if (!$h) $h = 1;
  91. try {
  92. $this->image->scaleImage($w, $h);
  93. $this->image->cropImage($width, $height, -$x, -$y);
  94. } catch (Exception $e) {
  95. return false;
  96. }
  97. $this->width = $width;
  98. $this->height = $height;
  99. return true;
  100. }
  101. public function rotate($angle, $background="#000000") {
  102. try {
  103. $this->image->rotateImage(new ImagickPixel($background), $angle);
  104. $size = $this->image->getImageGeometry();
  105. } catch (Exception $e) {
  106. return false;
  107. }
  108. $this->width = $size['width'];
  109. $this->height = $size['height'];
  110. return true;
  111. }
  112. public function flipHorizontal() {
  113. try {
  114. $this->image->flopImage();
  115. } catch (Exception $e) {
  116. return false;
  117. }
  118. return true;
  119. }
  120. public function flipVertical() {
  121. try {
  122. $this->image->flipImage();
  123. } catch (Exception $e) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. public function watermark($file, $left=false, $top=false) {
  129. try {
  130. $wm = new Imagick($file);
  131. $size = $wm->getImageGeometry();
  132. } catch (Exception $e) {
  133. return false;
  134. }
  135. $w = $size['width'];
  136. $h = $size['height'];
  137. $x =
  138. ($left === true) ? 0 : (
  139. ($left === null) ? round(($this->width - $w) / 2) : (
  140. (($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
  141. $y =
  142. ($top === true) ? 0 : (
  143. ($top === null) ? round(($this->height - $h) / 2) : (
  144. (($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
  145. if ((($x + $w) > $this->width) ||
  146. (($y + $h) > $this->height) ||
  147. ($x < 0) || ($y < 0)
  148. )
  149. return false;
  150. try {
  151. $this->image->compositeImage($wm, Imagick::COMPOSITE_DEFAULT, $x, $y);
  152. } catch (Exception $e) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. // ABSTRACT PROTECTED METHODS
  158. protected function getBlankImage($width, $height) {
  159. try {
  160. $img = new Imagick();
  161. $img->newImage($width, $height, "none");
  162. $img->setImageCompressionQuality(100);
  163. } catch (Exception $e) {
  164. return false;
  165. }
  166. return $img;
  167. }
  168. protected function getImage($image, &$width, &$height) {
  169. if (is_object($image) && ($image instanceof image_imagick)) {
  170. try {
  171. $image->image->setImageCompressionQuality(100);
  172. } catch (Exception $e) {
  173. return false;
  174. }
  175. $width = $image->width;
  176. $height = $image->height;
  177. return $image->image;
  178. } elseif (is_object($image) && ($image instanceof Imagick)) {
  179. try {
  180. $image->setImageCompressionQuality(100);
  181. $size = $image->getImageGeometry();
  182. } catch (Exception $e) {
  183. return false;
  184. }
  185. $width = $size['width'];
  186. $height = $size['height'];
  187. return $image;
  188. } elseif (is_string($image)) {
  189. try {
  190. $image = new Imagick($image);
  191. $image->setImageCompressionQuality(100);
  192. $size = $image->getImageGeometry();
  193. } catch (Exception $e) {
  194. return false;
  195. }
  196. $width = $size['width'];
  197. $height = $size['height'];
  198. return $image;
  199. } else
  200. return false;
  201. }
  202. // PSEUDO-ABSTRACT STATIC METHODS
  203. static function available() {
  204. return class_exists("Imagick");
  205. }
  206. static function checkImage($file) {
  207. try {
  208. $img = new Imagic($file);
  209. } catch (Exception $e) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. // INHERIT METHODS
  215. public function output($type="jpeg", array $options=array()) {
  216. $type = strtolower($type);
  217. try {
  218. $this->image->setImageFormat($type);
  219. } catch (Exception $e) {
  220. return false;
  221. }
  222. $method = "optimize_$type";
  223. if (method_exists($this, $method) && !$this->$method($options))
  224. return false;
  225. if (!isset($options['file'])) {
  226. if (!headers_sent()) {
  227. $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
  228. header("Content-Type: $mime");
  229. }
  230. echo $this->image;
  231. } else {
  232. $file = $options['file'] . ".$type";
  233. try {
  234. $this->image->writeImage($file);
  235. } catch (Exception $e) {
  236. @unlink($file);
  237. return false;
  238. }
  239. if (!@rename($file, $options['file'])) {
  240. @unlink($file);
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. // OWN METHODS
  247. protected function optimize_jpeg(array $options=array()) {
  248. $quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
  249. try {
  250. $this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
  251. $this->image->setImageCompressionQuality($quality);
  252. } catch (Exception $e) {
  253. return false;
  254. }
  255. return true;
  256. }
  257. }
  258. ?>