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

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

https://gitlab.com/ptisky/API_prestashop
PHP | 375 lines | 275 code | 64 blank | 36 comment | 73 complexity | b560605158c3b27a224f42957b9789f8 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 GD 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_gd extends image {
  36. // ABSTRACT PUBLIC METHODS
  37. public function resize($width, $height) {
  38. if (!$width) $width = 1;
  39. if (!$height) $height = 1;
  40. return (
  41. (false !== ($img = new image_gd(array($width, $height)))) &&
  42. $img->imageCopyResampled($this) &&
  43. (false !== ($this->image = $img->image)) &&
  44. (false !== ($this->width = $img->width)) &&
  45. (false !== ($this->height = $img->height))
  46. );
  47. }
  48. public function resizeFit($width, $height, $background=false) {
  49. if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height)))
  50. return true;
  51. if (!$width || (($height / $width) < ($this->height / $this->width))) {
  52. $h = $height;
  53. $w = round(($this->width * $h) / $this->height);
  54. } elseif (!$height || (($width / $height) < ($this->width / $this->height))) {
  55. $w = $width;
  56. $h = round(($this->height * $w) / $this->width);
  57. } else {
  58. $w = $width;
  59. $h = $height;
  60. }
  61. if (!$w) $w = 1;
  62. if (!$h) $h = 1;
  63. if ($background === false)
  64. return $this->resize($w, $h);
  65. else {
  66. $img = new image_gd(array($width, $height));
  67. $x = round(($width - $w) / 2);
  68. $y = round(($height - $h) / 2);
  69. if ((false === $this->resize($w, $h)) ||
  70. (false === $img->imageFilledRectangle(0, 0, $width, $height, $background)) ||
  71. (false === $img->imageCopyResampled($this->image, $x, $y, 0, 0, $w, $h))
  72. )
  73. return false;
  74. $this->image = $img->image;
  75. $this->width = $width;
  76. $this->height = $height;
  77. return true;
  78. }
  79. }
  80. public function resizeCrop($width, $height, $offset=false) {
  81. if (($this->width / $this->height) > ($width / $height)) {
  82. $h = $height;
  83. $w = ($this->width * $h) / $this->height;
  84. $y = 0;
  85. if ($offset !== false) {
  86. if ($offset > 0)
  87. $offset = -$offset;
  88. if (($w + $offset) <= $width)
  89. $offset = $width - $w;
  90. $x = $offset;
  91. } else
  92. $x = ($width - $w) / 2;
  93. } else {
  94. $w = $width;
  95. $h = ($this->height * $w) / $this->width;
  96. $x = 0;
  97. if ($offset !== false) {
  98. if ($offset > 0)
  99. $offset = -$offset;
  100. if (($h + $offset) <= $height)
  101. $offset = $height - $h;
  102. $y = $offset;
  103. } else
  104. $y = ($height - $h) / 2;
  105. }
  106. $x = round($x);
  107. $y = round($y);
  108. $w = round($w);
  109. $h = round($h);
  110. if (!$w) $w = 1;
  111. if (!$h) $h = 1;
  112. $return = (
  113. (false !== ($img = new image_gd(array($width, $height))))) &&
  114. (false !== ($img->imageCopyResampled($this->image, $x, $y, 0, 0, $w, $h))
  115. );
  116. if ($return) {
  117. $this->image = $img->image;
  118. $this->width = $w;
  119. $this->height = $h;
  120. }
  121. return $return;
  122. }
  123. public function rotate($angle, $background="#000000") {
  124. $angle = -$angle;
  125. $img = @imagerotate($this->image, $angle, $this->gdColor($background));
  126. if ($img === false)
  127. return false;
  128. $this->width = imagesx($img);
  129. $this->height = imagesy($img);
  130. $this->image = $img;
  131. return true;
  132. }
  133. public function flipHorizontal() {
  134. $img = imagecreatetruecolor($this->width, $this->height);
  135. if (imagecopyresampled($img, $this->image, 0, 0, ($this->width - 1), 0, $this->width, $this->height, -$this->width, $this->height))
  136. $this->image = $img;
  137. else
  138. return false;
  139. return true;
  140. }
  141. public function flipVertical() {
  142. $img = imagecreatetruecolor($this->width, $this->height);
  143. if (imagecopyresampled($img, $this->image, 0, 0, 0, ($this->height - 1), $this->width, $this->height, $this->width, -$this->height))
  144. $this->image = $img;
  145. else
  146. return false;
  147. return true;
  148. }
  149. public function watermark($file, $left=false, $top=false) {
  150. $info = getimagesize($file);
  151. list($w, $h, $t) = $info;
  152. if (!in_array($t, array(IMAGETYPE_PNG, IMAGETYPE_GIF)))
  153. return false;
  154. $imagecreate = ($t == IMAGETYPE_PNG) ? "imagecreatefrompng" : "imagecreatefromgif";
  155. if (!@imagealphablending($this->image, true) ||
  156. (false === ($wm = @$imagecreate($file)))
  157. )
  158. return false;
  159. $w = imagesx($wm);
  160. $h = imagesy($wm);
  161. $x =
  162. ($left === true) ? 0 : (
  163. ($left === null) ? round(($this->width - $w) / 2) : (
  164. (($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
  165. $y =
  166. ($top === true) ? 0 : (
  167. ($top === null) ? round(($this->height - $h) / 2) : (
  168. (($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
  169. if ((($x + $w) > $this->width) ||
  170. (($y + $h) > $this->height) ||
  171. ($x < 0) || ($y < 0)
  172. )
  173. return false;
  174. if (($wm === false) || !@imagecopy($this->image, $wm, $x, $y, 0, 0, $w, $h))
  175. return false;
  176. @imagealphablending($this->image, false);
  177. @imagesavealpha($this->image, true);
  178. return true;
  179. }
  180. public function output($type='jpeg', array $options=array()) {
  181. $method = "output_$type";
  182. if (!method_exists($this, $method))
  183. return false;
  184. return $this->$method($options);
  185. }
  186. // ABSTRACT PROTECTED METHODS
  187. protected function getBlankImage($width, $height) {
  188. $image = imagecreatetruecolor($width, $height);
  189. imagealphablending($image, false);
  190. imagesavealpha($image, true);
  191. return $image;
  192. }
  193. protected function getImage($image, &$width, &$height) {
  194. if (is_resource($image) && (get_resource_type($image) == "gd")) {
  195. $width = @imagesx($image);
  196. $height = @imagesy($image);
  197. imagealphablending($image, false);
  198. imagesavealpha($image, true);
  199. return $image;
  200. } elseif (is_string($image) &&
  201. (false !== (list($width, $height, $t) = @getimagesize($image)))
  202. ) {
  203. $image =
  204. ($t == IMAGETYPE_GIF) ? @imagecreatefromgif($image) : (
  205. ($t == IMAGETYPE_WBMP) ? @imagecreatefromwbmp($image) : (
  206. ($t == IMAGETYPE_JPEG) ? @imagecreatefromjpeg($image) : (
  207. ($t == IMAGETYPE_PNG) ? @imagecreatefrompng($image) : (
  208. ($t == IMAGETYPE_XBM) ? @imagecreatefromxbm($image) : false
  209. ))));
  210. if ($t == IMAGETYPE_PNG) {
  211. imagealphablending($image, false);
  212. imagesavealpha($image, true);
  213. }
  214. return $image;
  215. } else
  216. return false;
  217. }
  218. // PSEUDO-ABSTRACT STATIC METHODS
  219. static function available() {
  220. return function_exists("imagecreatefromjpeg");
  221. }
  222. static function checkImage($file) {
  223. if (!is_string($file) ||
  224. ((false === (list($width, $height, $t) = @getimagesize($file))))
  225. )
  226. return false;
  227. $img =
  228. ($t == IMAGETYPE_GIF) ? @imagecreatefromgif($file) : (
  229. ($t == IMAGETYPE_WBMP) ? @imagecreatefromwbmp($file) : (
  230. ($t == IMAGETYPE_JPEG) ? @imagecreatefromjpeg($file) : (
  231. ($t == IMAGETYPE_PNG) ? @imagecreatefrompng($file) : (
  232. ($t == IMAGETYPE_XBM) ? @imagecreatefromxbm($file) : false
  233. ))));
  234. return ($img !== false);
  235. }
  236. // OWN METHODS
  237. protected function output_png(array $options=array()) {
  238. $file = isset($options['file']) ? $options['file'] : null;
  239. $quality = isset($options['quality']) ? $options['quality'] : null;
  240. $filters = isset($options['filters']) ? $options['filters'] : null;
  241. if (($file === null) && !headers_sent())
  242. header("Content-Type: image/png");
  243. return imagepng($this->image, $file, $quality, $filters);
  244. }
  245. protected function output_jpeg(array $options=array()) {
  246. $file = isset($options['file']) ? $options['file'] : null;
  247. $quality = isset($options['quality'])
  248. ? $options['quality']
  249. : self::DEFAULT_JPEG_QUALITY;
  250. if (($file === null) && !headers_sent())
  251. header("Content-Type: image/jpeg");
  252. return imagejpeg($this->image, $file, $quality);
  253. }
  254. protected function output_gif(array $options=array()) {
  255. $file = isset($options['file']) ? $options['file'] : null;
  256. if (isset($options['file']) && !headers_sent())
  257. header("Content-Type: image/gif");
  258. return imagegif($this->image, $file);
  259. }
  260. protected function gdColor() {
  261. $args = func_get_args();
  262. $exprRGB = '/^rgb\(\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\)$/i';
  263. $exprHex1 = '/^\#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i';
  264. $exprHex2 = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i';
  265. $exprByte = '/^([01]?\d?\d|2[0-4]\d|25[0-5])$/';
  266. if (!isset($args[0]))
  267. return false;
  268. if (count($args[0]) == 3) {
  269. list($r, $g, $b) = $args[0];
  270. } elseif (preg_match($exprRGB, $args[0], $match)) {
  271. list($tmp, $r, $g, $b) = $match;
  272. } elseif (preg_match($exprHex1, $args[0], $match)) {
  273. list($tmp, $r, $g, $b) = $match;
  274. $r = hexdec($r);
  275. $g = hexdec($g);
  276. $b = hexdec($b);
  277. } elseif (preg_match($exprHex2, $args[0], $match)) {
  278. list($tmp, $r, $g, $b) = $match;
  279. $r = hexdec("$r$r");
  280. $g = hexdec("$g$g");
  281. $b = hexdec("$b$b");
  282. } elseif ((count($args) == 3) &&
  283. preg_match($exprByte, $args[0]) &&
  284. preg_match($exprByte, $args[1]) &&
  285. preg_match($exprByte, $args[2])
  286. ) {
  287. list($r, $g, $b) = $args;
  288. } else
  289. return false;
  290. return imagecolorallocate($this->image, $r, $g, $b);
  291. }
  292. protected function imageFilledRectangle($x1, $y1, $x2, $y2, $color) {
  293. $color = $this->gdColor($color);
  294. if ($color === false) return false;
  295. return imageFilledRectangle($this->image, $x1, $y1, $x2, $y2, $color);
  296. }
  297. protected function imageCopyResampled(
  298. $src, $dstX=0, $dstY=0, $srcX=0, $srcY=0, $dstW=null, $dstH=null, $srcW=null, $srcH=null
  299. ) {
  300. $imageDetails = $this->buildImage($src);
  301. if ($imageDetails === false)
  302. return false;
  303. list($src, $srcWidth, $srcHeight) = $imageDetails;
  304. if (is_null($dstW)) $dstW = $this->width - $dstW;
  305. if (is_null($dstH)) $dstH = $this->height - $dstY;
  306. if (is_null($srcW)) $srcW = $srcWidth - $srcX;
  307. if (is_null($srcH)) $srcH = $srcHeight - $srcY;
  308. return imageCopyResampled($this->image, $src, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  309. }
  310. }
  311. ?>