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

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

https://gitlab.com/ptisky/API_prestashop
PHP | 325 lines | 239 code | 48 blank | 38 comment | 31 complexity | 22428a0e09f060d1ec8109f26959e39b 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 GraphicsMagick 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_gmagick 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. $w = $this->image->getImageWidth();
  58. $h = $this->image->getImageHeight();
  59. } catch (\Exception $e) {
  60. return false;
  61. }
  62. if ($background === false) {
  63. $this->width = $w;
  64. $this->height = $h;
  65. return true;
  66. } else {
  67. try {
  68. $this->image->setImageBackgroundColor($background);
  69. $x = round(($width - $w) / 2);
  70. $y = round(($height - $h) / 2);
  71. $img = new \Gmagick();
  72. $img->newImage($width, $height, $background);
  73. $img->compositeImage($this->image, 1, $x, $y);
  74. } catch (\Exception $e) {
  75. return false;
  76. }
  77. $this->image = $img;
  78. $this->width = $width;
  79. $this->height = $height;
  80. return true;
  81. }
  82. }
  83. public function resizeCrop($width, $height, $offset=false) {
  84. if (!$width) $width = 1;
  85. if (!$height) $height = 1;
  86. if (($this->width / $this->height) > ($width / $height)) {
  87. $h = $height;
  88. $w = ($this->width * $h) / $this->height;
  89. $y = 0;
  90. if ($offset !== false) {
  91. if ($offset > 0)
  92. $offset = -$offset;
  93. if (($w + $offset) <= $width)
  94. $offset = $width - $w;
  95. $x = $offset;
  96. } else
  97. $x = ($width - $w) / 2;
  98. } else {
  99. $w = $width;
  100. $h = ($this->height * $w) / $this->width;
  101. $x = 0;
  102. if ($offset !== false) {
  103. if ($offset > 0)
  104. $offset = -$offset;
  105. if (($h + $offset) <= $height)
  106. $offset = $height - $h;
  107. $y = $offset;
  108. } else
  109. $y = ($height - $h) / 2;
  110. }
  111. $x = round($x);
  112. $y = round($y);
  113. $w = round($w);
  114. $h = round($h);
  115. if (!$w) $w = 1;
  116. if (!$h) $h = 1;
  117. try {
  118. $this->image->scaleImage($w, $h);
  119. $this->image->cropImage($width, $height, -$x, -$y);
  120. } catch (\Exception $e) {
  121. return false;
  122. }
  123. $this->width = $width;
  124. $this->height = $height;
  125. return true;
  126. }
  127. public function rotate($angle, $background="#000000") {
  128. try {
  129. $this->image->rotateImage($background, $angle);
  130. $w = $this->image->getImageWidth();
  131. $h = $this->image->getImageHeight();
  132. } catch (\Exception $e) {
  133. return false;
  134. }
  135. $this->width = $w;
  136. $this->height = $h;
  137. return true;
  138. }
  139. public function flipHorizontal() {
  140. try {
  141. $this->image->flopImage();
  142. } catch (\Exception $e) {
  143. return false;
  144. }
  145. return true;
  146. }
  147. public function flipVertical() {
  148. try {
  149. $this->image->flipImage();
  150. } catch (\Exception $e) {
  151. return false;
  152. }
  153. return true;
  154. }
  155. public function watermark($file, $left=false, $top=false) {
  156. try {
  157. $wm = new \Gmagick($file);
  158. $w = $wm->getImageWidth();
  159. $h = $wm->getImageHeight();
  160. } catch (\Exception $e) {
  161. return false;
  162. }
  163. $x =
  164. ($left === true) ? 0 : (
  165. ($left === null) ? round(($this->width - $w) / 2) : (
  166. (($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
  167. $y =
  168. ($top === true) ? 0 : (
  169. ($top === null) ? round(($this->height - $h) / 2) : (
  170. (($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
  171. if ((($x + $w) > $this->width) ||
  172. (($y + $h) > $this->height) ||
  173. ($x < 0) || ($y < 0)
  174. )
  175. return false;
  176. try {
  177. $this->image->compositeImage($wm, 1, $x, $y);
  178. } catch (\Exception $e) {
  179. return false;
  180. }
  181. return true;
  182. }
  183. // ABSTRACT PROTECTED METHODS
  184. protected function getBlankImage($width, $height) {
  185. try {
  186. $img = new \Gmagick();
  187. $img->newImage($width, $height, "none");
  188. } catch (\Exception $e) {
  189. return false;
  190. }
  191. return $img;
  192. }
  193. protected function getImage($image, &$width, &$height) {
  194. if (is_object($image) && ($image instanceof image_gmagick)) {
  195. $width = $image->width;
  196. $height = $image->height;
  197. return $image->image;
  198. } elseif (is_object($image) && ($image instanceof \Gmagick)) {
  199. try {
  200. $w = $image->getImageWidth();
  201. $h = $image->getImageHeight();
  202. } catch (\Exception $e) {
  203. return false;
  204. }
  205. $width = $w;
  206. $height = $h;
  207. return $image;
  208. } elseif (is_string($image)) {
  209. try {
  210. $image = new \Gmagick($image);
  211. $w = $image->getImageWidth();
  212. $h = $image->getImageHeight();
  213. } catch (\Exception $e) {
  214. return false;
  215. }
  216. $width = $w;
  217. $height = $h;
  218. return $image;
  219. } else
  220. return false;
  221. }
  222. // PSEUDO-ABSTRACT STATIC METHODS
  223. static function available() {
  224. return class_exists("Gmagick");
  225. }
  226. static function checkImage($file) {
  227. try {
  228. $img = new \Gmagick($file);
  229. } catch (\Exception $e) {
  230. return false;
  231. }
  232. return true;
  233. }
  234. // INHERIT METHODS
  235. public function output($type="jpeg", array $options=array()) {
  236. $type = strtolower($type);
  237. try {
  238. $this->image->setImageFormat($type);
  239. } catch (\Exception $e) {
  240. return false;
  241. }
  242. $method = "optimize_$type";
  243. if (method_exists($this, $method) && !$this->$method($options))
  244. return false;
  245. if (!isset($options['file'])) {
  246. if (!headers_sent()) {
  247. $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
  248. header("Content-Type: $mime");
  249. }
  250. echo $this->image;
  251. } else {
  252. $file = $options['file'] . ".$type";
  253. try {
  254. $this->image->writeImage($file);
  255. } catch (\Exception $e) {
  256. @unlink($file);
  257. return false;
  258. }
  259. if (!@rename($file, $options['file'])) {
  260. @unlink($file);
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. // OWN METHODS
  267. protected function optimize_jpeg(array $options=array()) {
  268. $quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
  269. try {
  270. $this->image->setCompressionQuality($quality);
  271. } catch (\Exception $e) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. }
  277. ?>