PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/js/tiny_mce/plugins/kcfinder/lib/class_image_gd.php

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