PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/kcfinder/lib/class_image_imagick.php

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