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

/public/js/kcfinder/lib/class_image_gd.php

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