PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/js/plugins/kcfinder-1.7/class/class_gd.php

https://github.com/mydesignivan/guerraguerrero
PHP | 381 lines | 230 code | 79 blank | 72 comment | 49 complexity | 6da95dd4aa34f726c43d291642fba53b MD5 | raw file
  1. <?php
  2. /** This file is part KCFinder project
  3. *
  4. * @desc GD extension class
  5. * @package KCFinder
  6. * @version 1.7
  7. * @author Pavel Tzonkov <pavelc@users.sf.net>
  8. * @copyright 2010 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 gd {
  14. /** GD resource
  15. * @var resource */
  16. protected $image;
  17. /** Image width
  18. * @var integer */
  19. protected $width;
  20. /** Image height
  21. * @var integer */
  22. protected $height;
  23. /** Init error
  24. * @var bool */
  25. public $init_error = false;
  26. /** Last builded image type constant (IMAGETYPE_XXX)
  27. * @var integer */
  28. public $type;
  29. /** Returns an array. Element 0 - GD resource. Element 1 - width. Element 2 - height.
  30. * Returns FALSE on failure. The only one paramaeter $image can be an instance of this class,
  31. * a GD resource, an array(width, height) or path to image file.
  32. * @param mixed $image
  33. * @return array */
  34. protected function build_image($image) {
  35. if ($image instanceof gd) {
  36. $width = $image->get_width();
  37. $height = $image->get_height();
  38. $image = $image->get_image();
  39. } elseif (is_resource($image) && (get_resource_type($image) == "gd")) {
  40. $width = imagesx($image);
  41. $height = imagesy($image);
  42. } elseif (is_array($image)) {
  43. list($key, $width) = each($image);
  44. list($key, $height) = each($image);
  45. $image = imagecreatetruecolor($width, $height);
  46. } elseif (false !== (list($width, $height, $type) = getimagesize($image)))
  47. $image =
  48. ($type == IMAGETYPE_GIF) ? @imagecreatefromgif($image) : (
  49. ($type == IMAGETYPE_WBMP) ? @imagecreatefromwbmp($image) : (
  50. ($type == IMAGETYPE_JPEG) ? @imagecreatefromjpeg($image) : (
  51. ($type == IMAGETYPE_JPEG2000) ? @imagecreatefromjpeg($image) : (
  52. ($type == IMAGETYPE_PNG) ? @imagecreatefrompng($image) : (
  53. ($type == IMAGETYPE_XBM) ? @imagecreatefromxbm($image) : false
  54. )))));
  55. $return = (
  56. is_resource($image) &&
  57. (get_resource_type($image) == "gd") &&
  58. isset($width) &&
  59. isset($height) &&
  60. (preg_match('/^[1-9][0-9]*$/', $width) !== false) &&
  61. (preg_match('/^[1-9][0-9]*$/', $height) !== false)
  62. )
  63. ? array($image, $width, $height)
  64. : false;
  65. if (($return !== false) && isset($type))
  66. $this->type = $type;
  67. return $return;
  68. }
  69. /** Parameter $image can be:
  70. * 1. An instance of this class (copy instance).
  71. * 2. A GD resource.
  72. * 3. An array with two elements. First - width, second - height. Create a blank image.
  73. * 4. A filename string. Get image form file.
  74. * The non-required parameter $bigger_size is the bigger dimension (width or height) the image
  75. * will be resized to. The other dimension (height or width) will be calculated autamaticaly
  76. * @param mixed $image
  77. * @param integer $bigger_size
  78. * @return gd */
  79. public function __construct($image, $bigger_size=null) {
  80. $this->image = $this->width = $this->height = null;
  81. $image_details = $this->build_image($image);
  82. if ($image_details !== false)
  83. list($this->image, $this->width, $this->height) = $image_details;
  84. else
  85. $this->init_error = true;
  86. if (!is_null($this->image) &&
  87. !is_null($bigger_size) &&
  88. (preg_match('/^[1-9][0-9]*$/', $bigger_size) !== false)
  89. ) {
  90. $image = $this->image;
  91. list($width, $height) = $this->get_prop_size($bigger_size);
  92. $this->image = imagecreatetruecolor($width, $height);
  93. $this->width = $width;
  94. $this->height = $height;
  95. $this->imagecopyresampled($image);
  96. }
  97. }
  98. /** Returns the GD resource
  99. * @return resource */
  100. public function get_image() {
  101. return $this->image;
  102. }
  103. /** Returns the image width
  104. * @return integer */
  105. public function get_width() {
  106. return $this->width;
  107. }
  108. /** Returns the image height
  109. * @return integer */
  110. public function get_height() {
  111. return $this->height;
  112. }
  113. /** Returns calculated proportional width from the given height
  114. * @param integer $resized_height
  115. * @return integer */
  116. public function get_prop_width($resized_height) {
  117. return intval(($this->width * $resized_height) / $this->height);
  118. }
  119. /** Returns calculated proportional height from the given width
  120. * @param integer $resized_width
  121. * @return integer */
  122. public function get_prop_height($resized_width) {
  123. return intval(($this->height * $resized_width) / $this->width);
  124. }
  125. /** Returns an array with calculated proportional width & height.
  126. * The parameter $bigger_size is the bigger dimension (width or height) of calculated sizes.
  127. * The other dimension (height or width) will be calculated autamaticaly
  128. * @param integer $bigger_size
  129. * @return array */
  130. public function get_prop_size($bigger_size) {
  131. if ($this->width > $this->height) {
  132. $width = $bigger_size;
  133. $height = $this->get_prop_height($width);
  134. } elseif ($this->height > $this->width) {
  135. $height = $bigger_size;
  136. $width = $this->get_prop_width($height);
  137. } else
  138. $width = $height = $bigger_size;
  139. return array($width, $height);
  140. }
  141. /** Resize image. Returns TRUE on success or FALSE on failure
  142. * @param integer $width
  143. * @param integer $height
  144. * @return bool */
  145. public function resize($width, $height) {
  146. return (
  147. (false !== ($img = new gd(array($width, $height)))) &&
  148. $img->imagecopyresampled($this) &&
  149. (false !== ($this->image = $img->get_image())) &&
  150. (false !== ($this->width = $img->get_width())) &&
  151. (false !== ($this->height = $img->get_height()))
  152. );
  153. }
  154. /** Resize the given image source (GD, gd object or image file path) to fit in the own image.
  155. * The outside ares will be cropped out. Returns TRUE on success or FALSE on failure
  156. * @param mixed $src
  157. * @return bool */
  158. public function resize_crop($src) {
  159. $image_details = $this->build_image($src);
  160. if ($image_details !== false) {
  161. list($src, $src_width, $src_height) = $image_details;
  162. if (($src_width / $src_height) > ($this->width / $this->height)) {
  163. $src_w = $this->get_prop_width($src_height);
  164. $src_h = $src_height;
  165. $src_x = intval(($src_width - $src_w) / 2);
  166. $src_y = 0;
  167. } else {
  168. $src_w = $src_width;
  169. $src_h = $this->get_prop_height($src_width);
  170. $src_x = 0;
  171. $src_y = intval(($src_height - $src_h) / 2);
  172. }
  173. return imagecopyresampled($this->image, $src, 0, 0, $src_x, $src_y, $this->width, $this->height, $src_w, $src_h);
  174. } else
  175. return false;
  176. }
  177. /** Resize image to fit in given resolution. Returns TRUE on success or FALSE on failure
  178. * @param integer $width
  179. * @param integer $height
  180. * @return bool */
  181. public function resize_fit($width, $height) {
  182. if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height)))
  183. return true;
  184. if (!$width || (($width / $height) > ($this->width / $this->height)))
  185. $width = intval(($this->width * $height) / $this->height);
  186. elseif (!$height || (($width / $height) < ($this->width / $this->height)))
  187. $height = intval(($this->height * $width) / $this->width);
  188. return $this->resize($width, $height);
  189. }
  190. /** Neka si predstavim vyobrazhaem pravoygylnik s razmeri $width i $height.
  191. * Izobrazhenieto shte se preorazmeri taka che to shte izliza ot tozi pravoygylnik,
  192. * no samo po edno (x ili y) izmerenie
  193. * @param integer $width
  194. * @param integer $height
  195. * @return bool */
  196. public function resize_overflow($width, $height) {
  197. $big = (($this->width / $this->height) > ($width / $height))
  198. ? ($this->width * $height) / $this->height
  199. : ($this->height * $width) / $this->width;
  200. $big = intval($big);
  201. $return = ($img = new gd($this->image, $big));
  202. if ($return) {
  203. $this->image = $img->get_image();
  204. $this->width = $img->get_width();
  205. $this->height = $img->get_height();
  206. }
  207. return $return;
  208. }
  209. public function gd_color() {
  210. $args = func_get_args();
  211. $expr_rgb = '/^rgb\(\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\)$/i';
  212. $expr_hex1 = '/^\#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i';
  213. $expr_hex2 = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i';
  214. $expr_byte = '/^([01]?\d?\d|2[0-4]\d|25[0-5])$/';
  215. if (!isset($args[0]))
  216. return false;
  217. if (count($args[0]) == 3) {
  218. list($r, $g, $b) = $args[0];
  219. } elseif (preg_match($expr_rgb, $args[0])) {
  220. list($r, $g, $b) = explode(" ", preg_replace($expr_rgb, "$1 $2 $3", $args[0]));
  221. } elseif (preg_match($expr_hex1, $args[0])) {
  222. list($r, $g, $b) = explode(" ", preg_replace($expr_hex1, "$1 $2 $3", $args[0]));
  223. $r = hexdec($r);
  224. $g = hexdec($g);
  225. $b = hexdec($b);
  226. } elseif (preg_match($expr_hex2, $args[0])) {
  227. list($r, $g, $b) = explode(" ", preg_replace($expr_hex2, "$1$1 $2$2 $3$3", $args[0]));
  228. $r = hexdec($r);
  229. $g = hexdec($g);
  230. $b = hexdec($b);
  231. } elseif ((count($args) == 3) &&
  232. preg_match($expr_byte, $args[0]) &&
  233. preg_match($expr_byte, $args[1]) &&
  234. preg_match($expr_byte, $args[2])
  235. ) {
  236. list($r, $g, $b) = $args;
  237. } else
  238. return false;
  239. return imagecolorallocate($this->image, $r, $g, $b);
  240. }
  241. public function fill_color($color) {
  242. return $this->imagefilledrectangle(0, 0, $this->width - 1, $this->height - 1, $color);
  243. }
  244. /* G D M E T H O D S */
  245. public function imagecopy(
  246. $src,
  247. $dst_x=0, $dst_y=0,
  248. $src_x=0, $src_y=0,
  249. $dst_w=null, $dst_h=null,
  250. $src_w=null, $src_h=null
  251. ) {
  252. $image_details = $this->build_image($src);
  253. if ($image_details !== false) {
  254. list($src, $src_width, $src_height) = $image_details;
  255. if (is_null($dst_w)) $dst_w = $this->width - $dst_x;
  256. if (is_null($dst_h)) $dst_h = $this->height - $dst_y;
  257. if (is_null($src_w)) $src_w = $src_width - $src_x;
  258. if (is_null($src_h)) $src_h = $src_height - $src_y;
  259. return imagecopy($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  260. } else
  261. return false;
  262. }
  263. public function imagecopyresampled(
  264. $src,
  265. $dst_x=0, $dst_y=0,
  266. $src_x=0, $src_y=0,
  267. $dst_w=null, $dst_h=null,
  268. $src_w=null, $src_h=null
  269. ) {
  270. $image_details = $this->build_image($src);
  271. if ($image_details !== false) {
  272. list($src, $src_width, $src_height) = $image_details;
  273. if (is_null($dst_w)) $dst_w = $this->width - $dst_x;
  274. if (is_null($dst_h)) $dst_h = $this->height - $dst_y;
  275. if (is_null($src_w)) $src_w = $src_width - $src_x;
  276. if (is_null($src_h)) $src_h = $src_height - $src_y;
  277. return imagecopyresampled($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  278. } else
  279. return false;
  280. }
  281. public function imagefilledrectangle($x1, $y1, $x2, $y2, $color) {
  282. $color = $this->gd_color($color);
  283. if ($color === false) return false;
  284. return imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $color);
  285. }
  286. public function imagepng($filename=null, $quality=null, $filters=null) {
  287. if (is_null($filename) && !headers_sent())
  288. header("Content-Type: image/png");
  289. return imagepng($this->image, $filename, $quality, $filters);
  290. }
  291. public function imagejpeg($filename=null, $quality=75) {
  292. if (is_null($filename) && !headers_sent())
  293. header("Content-Type: image/jpeg");
  294. return imagejpeg($this->image, $filename, $quality);
  295. }
  296. public function imagegif($filename=null) {
  297. if (is_null($filename) && !headers_sent())
  298. header("Content-Type: image/gif");
  299. return imagegif($this->image, $filename);
  300. }
  301. }
  302. ?>