/modules/storecommander/ead6f6fce09/SC/lib/js/ckeditor/kcfinder/lib/class_image_imagick.php

https://gitlab.com/ptisky/API_prestashop · PHP · 328 lines · 242 code · 48 blank · 38 comment · 31 complexity · 9a0bcc151e76417555c75f4b38ee0cad MD5 · raw file

  1. <?php
  2. /**
  3. * Store Commander
  4. *
  5. * @category administration
  6. * @author Store Commander - support@storecommander.com
  7. * @version 2015-09-15
  8. * @uses Prestashop modules
  9. * @since 2009
  10. * @copyright Copyright &copy; 2009-2015, Store Commander
  11. * @license commercial
  12. * All rights reserved! Copying, duplication strictly prohibited
  13. *
  14. * *****************************************
  15. * * STORE COMMANDER *
  16. * * http://www.StoreCommander.com *
  17. * * V 2015-09-15 *
  18. * *****************************************
  19. *
  20. * Compatibility: PS version: 1.1 to 1.6.1
  21. *
  22. **/
  23. /** This file is part of KCFinder project
  24. *
  25. * @desc ImageMagick image driver class
  26. * @package KCFinder
  27. * @version 3.12
  28. * @author Pavel Tzonkov <sunhater@sunhater.com>
  29. * @copyright 2010-2014 KCFinder Project
  30. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  31. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  32. * @link http://kcfinder.sunhater.com
  33. */
  34. namespace kcfinder;
  35. class image_imagick extends image {
  36. static $MIMES = array(
  37. //'tif' => "image/tiff"
  38. );
  39. // ABSTRACT PUBLIC METHODS
  40. public function resize($width, $height) {//
  41. if (!$width) $width = 1;
  42. if (!$height) $height = 1;
  43. try {
  44. $this->image->scaleImage($width, $height);
  45. } catch (\Exception $e) {
  46. return false;
  47. }
  48. $this->width = $width;
  49. $this->height = $height;
  50. return true;
  51. }
  52. public function resizeFit($width, $height, $background=false) {//
  53. if (!$width) $width = 1;
  54. if (!$height) $height = 1;
  55. try {
  56. $this->image->scaleImage($width, $height, true);
  57. $size = $this->image->getImageGeometry();
  58. } catch (\Exception $e) {
  59. return false;
  60. }
  61. if ($background === false) {
  62. $this->width = $size['width'];
  63. $this->height = $size['height'];
  64. return true;
  65. } else {
  66. try {
  67. $this->image->setImageBackgroundColor($background);
  68. $x = -round(($width - $size['width']) / 2);
  69. $y = -round(($height - $size['height']) / 2);
  70. $this->image->extentImage($width, $height, $x, $y);
  71. } catch (\Exception $e) {
  72. return false;
  73. }
  74. $this->width = $width;
  75. $this->height = $height;
  76. return true;
  77. }
  78. }
  79. public function resizeCrop($width, $height, $offset=false) {
  80. if (!$width) $width = 1;
  81. if (!$height) $height = 1;
  82. if (($this->width / $this->height) > ($width / $height)) {
  83. $h = $height;
  84. $w = ($this->width * $h) / $this->height;
  85. $y = 0;
  86. if ($offset !== false) {
  87. if ($offset > 0)
  88. $offset = -$offset;
  89. if (($w + $offset) <= $width)
  90. $offset = $width - $w;
  91. $x = $offset;
  92. } else
  93. $x = ($width - $w) / 2;
  94. } else {
  95. $w = $width;
  96. $h = ($this->height * $w) / $this->width;
  97. $x = 0;
  98. if ($offset !== false) {
  99. if ($offset > 0)
  100. $offset = -$offset;
  101. if (($h + $offset) <= $height)
  102. $offset = $height - $h;
  103. $y = $offset;
  104. } else
  105. $y = ($height - $h) / 2;
  106. }
  107. $x = round($x);
  108. $y = round($y);
  109. $w = round($w);
  110. $h = round($h);
  111. if (!$w) $w = 1;
  112. if (!$h) $h = 1;
  113. try {
  114. $this->image->scaleImage($w, $h);
  115. $this->image->cropImage($width, $height, -$x, -$y);
  116. } catch (\Exception $e) {
  117. return false;
  118. }
  119. $this->width = $width;
  120. $this->height = $height;
  121. return true;
  122. }
  123. public function rotate($angle, $background="#000000") {
  124. try {
  125. $this->image->rotateImage(new \ImagickPixel($background), $angle);
  126. $size = $this->image->getImageGeometry();
  127. } catch (\Exception $e) {
  128. return false;
  129. }
  130. $this->width = $size['width'];
  131. $this->height = $size['height'];
  132. return true;
  133. }
  134. public function flipHorizontal() {
  135. try {
  136. $this->image->flopImage();
  137. } catch (\Exception $e) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. public function flipVertical() {
  143. try {
  144. $this->image->flipImage();
  145. } catch (\Exception $e) {
  146. return false;
  147. }
  148. return true;
  149. }
  150. public function watermark($file, $left=false, $top=false) {
  151. try {
  152. $wm = new \Imagick($file);
  153. $size = $wm->getImageGeometry();
  154. } catch (\Exception $e) {
  155. return false;
  156. }
  157. $w = $size['width'];
  158. $h = $size['height'];
  159. $x =
  160. ($left === true) ? 0 : (
  161. ($left === null) ? round(($this->width - $w) / 2) : (
  162. (($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
  163. $y =
  164. ($top === true) ? 0 : (
  165. ($top === null) ? round(($this->height - $h) / 2) : (
  166. (($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
  167. if ((($x + $w) > $this->width) ||
  168. (($y + $h) > $this->height) ||
  169. ($x < 0) || ($y < 0)
  170. )
  171. return false;
  172. try {
  173. $this->image->compositeImage($wm, \Imagick::COMPOSITE_DEFAULT, $x, $y);
  174. } catch (\Exception $e) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. // ABSTRACT PROTECTED METHODS
  180. protected function getBlankImage($width, $height) {
  181. try {
  182. $img = new \Imagick();
  183. $img->newImage($width, $height, "none");
  184. $img->setImageCompressionQuality(100);
  185. } catch (\Exception $e) {
  186. return false;
  187. }
  188. return $img;
  189. }
  190. protected function getImage($image, &$width, &$height) {
  191. if (is_object($image) && ($image instanceof image_imagick)) {
  192. try {
  193. $image->image->setImageCompressionQuality(100);
  194. } catch (\Exception $e) {
  195. return false;
  196. }
  197. $width = $image->width;
  198. $height = $image->height;
  199. return $image->image;
  200. } elseif (is_object($image) && ($image instanceof \Imagick)) {
  201. try {
  202. $image->setImageCompressionQuality(100);
  203. $size = $image->getImageGeometry();
  204. } catch (\Exception $e) {
  205. return false;
  206. }
  207. $width = $size['width'];
  208. $height = $size['height'];
  209. return $image;
  210. } elseif (is_string($image)) {
  211. try {
  212. $image = new \Imagick($image);
  213. $image->setImageCompressionQuality(100);
  214. $size = $image->getImageGeometry();
  215. } catch (\Exception $e) {
  216. return false;
  217. }
  218. $width = $size['width'];
  219. $height = $size['height'];
  220. return $image;
  221. } else
  222. return false;
  223. }
  224. // PSEUDO-ABSTRACT STATIC METHODS
  225. static function available() {
  226. return class_exists("\\Imagick");
  227. }
  228. static function checkImage($file) {
  229. try {
  230. $img = new \Imagick($file);
  231. } catch (\Exception $e) {
  232. return false;
  233. }
  234. return true;
  235. }
  236. // INHERIT METHODS
  237. public function output($type="jpeg", array $options=array()) {
  238. $type = strtolower($type);
  239. try {
  240. $this->image->setImageFormat($type);
  241. } catch (\Exception $e) {
  242. return false;
  243. }
  244. $method = "optimize_$type";
  245. if (method_exists($this, $method) && !$this->$method($options))
  246. return false;
  247. if (!isset($options['file'])) {
  248. if (!headers_sent()) {
  249. $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
  250. header("Content-Type: $mime");
  251. }
  252. echo $this->image;
  253. } else {
  254. $file = $options['file'] . ".$type";
  255. try {
  256. $this->image->writeImage($file);
  257. } catch (\Exception $e) {
  258. @unlink($file);
  259. return false;
  260. }
  261. if (!@rename($file, $options['file'])) {
  262. @unlink($file);
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. // OWN METHODS
  269. protected function optimize_jpeg(array $options=array()) {
  270. $quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
  271. try {
  272. $this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
  273. $this->image->setImageCompressionQuality($quality);
  274. } catch (\Exception $e) {
  275. return false;
  276. }
  277. return true;
  278. }
  279. }
  280. ?>