PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/web/class.gd.php

https://github.com/ilmich/clydephp
PHP | 256 lines | 200 code | 40 blank | 16 comment | 52 complexity | e00840b5bc4a3a41b52993285a3d655e MD5 | raw file
  1. <?php if (!defined('CLYDEPHP')) { header ('HTTP/1.1 404 Not Found'); exit(1); }
  2. // Simple wrapper class for GD
  3. class GD {
  4. public $im = null;
  5. public $width = null;
  6. public $height = null;
  7. public $type = null;
  8. public $mime = null;
  9. public function __construct($data = null, $ext = null) {
  10. if(is_resource($data) && get_resource_type($data) == 'gd')
  11. return $this->loadResource($data);
  12. elseif(@file_exists($data) && is_readable($data))
  13. return $this->loadFile($data);
  14. elseif(is_string($data))
  15. return $this->loadString($data);
  16. else
  17. return false;
  18. }
  19. private function loadResource($im) {
  20. if(!is_resource($im) || !get_resource_type($im) == 'gd')
  21. return false;
  22. $this->im = $im;
  23. $this->width = imagesx($im);
  24. $this->height = imagesy($im);
  25. return true;
  26. }
  27. private function loadFile($filename) {
  28. if(!file_exists($filename) || !is_readable($filename))
  29. return false;
  30. $info = getimagesize($filename);
  31. $this->width = $info[0];
  32. $this->height = $info[1];
  33. $this->type = image_type_to_extension($info[2], false);
  34. $this->mime = $info['mime'];
  35. if($this->type == 'jpeg' && (imagetypes() & IMG_JPG))
  36. $this->im = imagecreatefromjpeg($filename);
  37. elseif($this->type == 'png' && (imagetypes() & IMG_PNG))
  38. $this->im = imagecreatefrompng($filename);
  39. elseif($this->type == 'gif' && (imagetypes() & IMG_GIF))
  40. $this->im = imagecreatefromgif($filename);
  41. else
  42. return false;
  43. return true;
  44. }
  45. private function loadString($str) {
  46. $im = imagecreatefromstring($str);
  47. return ($im === false) ? false : $this->loadResource($im);
  48. }
  49. public function saveAs($filename, $type = 'jpg', $quality = 75) {
  50. if($type == 'jpg' && (imagetypes() & IMG_JPG))
  51. return imagejpeg($this->im, $filename, $quality);
  52. elseif($type == 'png' && (imagetypes() & IMG_PNG))
  53. return imagepng($this->im, $filename);
  54. elseif($type == 'gif' && (imagetypes() & IMG_GIF))
  55. return imagegif($this->im, $filename);
  56. else
  57. return false;
  58. }
  59. // Output file to browser
  60. public function output($type = 'jpg', $quality = 75) {
  61. if($type == 'jpg' && (imagetypes() & IMG_JPG)) {
  62. header("Content-Type: image/jpeg");
  63. imagejpeg($this->im, null, $quality);
  64. return true;
  65. }
  66. elseif($type == 'png' && (imagetypes() & IMG_PNG)) {
  67. header("Content-Type: image/png");
  68. imagepng($this->im);
  69. return true;
  70. }
  71. elseif($type == 'gif' && (imagetypes() & IMG_GIF)) {
  72. header("Content-Type: image/gif");
  73. imagegif($this->im);
  74. return true;
  75. }
  76. else
  77. return false;
  78. }
  79. // Return image data as a string.
  80. // Is there a way to do this without using output buffering?
  81. public function tostring($type = 'jpg', $quality = 75) {
  82. ob_start();
  83. if($type == 'jpg' && (imagetypes() & IMG_JPG))
  84. imagejpeg($this->im, null, $quality);
  85. elseif($type == 'png' && (imagetypes() & IMG_PNG))
  86. imagepng($this->im);
  87. elseif($type == 'gif' && (imagetypes() & IMG_GIF))
  88. imagegif($this->im);
  89. return ob_get_clean();
  90. }
  91. // Resizes an image and maintains aspect ratio.
  92. public function scale($new_width = null, $new_height = null) {
  93. if(!is_null($new_width) && is_null($new_height))
  94. $new_height = $new_width * $this->height / $this->width;
  95. elseif(is_null($new_width) && !is_null($new_height))
  96. $new_width = $this->width / $this->height * $new_height;
  97. elseif(!is_null($new_width) && !is_null($new_height)) {
  98. if($this->width < $this->height)
  99. $new_width = $this->width / $this->height * $new_height;
  100. else
  101. $new_height = $new_width * $this->height / $this->width;
  102. }
  103. else
  104. return false;
  105. return $this->resize($new_width, $new_height);
  106. }
  107. // Resizes an image to an exact size
  108. public function resize($new_width, $new_height) {
  109. $dest = imagecreatetruecolor($new_width, $new_height);
  110. // Transparency fix contributed by Google Code user 'desfrenes'
  111. imagealphablending($dest, false);
  112. imagesavealpha($dest, true);
  113. if(imagecopyresampled($dest, $this->im, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height)) {
  114. $this->im = $dest;
  115. $this->width = imagesx($this->im);
  116. $this->height = imagesy($this->im);
  117. return true;
  118. }
  119. return false;
  120. }
  121. public function crop($x, $y, $w, $h) {
  122. $dest = imagecreatetruecolor($w, $h);
  123. if(imagecopyresampled($dest, $this->im, 0, 0, $x, $y, $w, $h, $w, $h)) {
  124. $this->im = $dest;
  125. $this->width = $w;
  126. $this->height = $h;
  127. return true;
  128. }
  129. return false;
  130. }
  131. public function cropCentered($w, $h) {
  132. $cx = $this->width / 2;
  133. $cy = $this->height / 2;
  134. $x = $cx - $w / 2;
  135. $y = $cy - $h / 2;
  136. if($x < 0)
  137. $x = 0;
  138. if($y < 0)
  139. $y = 0;
  140. return $this->crop($x, $y, $w, $h);
  141. }
  142. public function gaussianBlur() {
  143. $matrix = array(array(1.0, 2.0, 1.0),
  144. array(2.0, 4.0, 2.0),
  145. array(1.0, 2.0, 1.0));
  146. $this->_applyConvolutionMatrix($matrix);
  147. }
  148. public function sharpen() {
  149. $matrix = array(array(0,-1, 0),
  150. array(-1, 5, -1),
  151. array(0, -1, 0));
  152. $this->_applyConvolutionMatrix($matrix);
  153. }
  154. public function blur() {
  155. $matrix = array(array(1,1, 1),
  156. array(1, 1, 1),
  157. array(1, 1, 1));
  158. $this->_applyConvolutionMatrix($matrix);
  159. }
  160. public function edgeEnhance() {
  161. $matrix = array(array(0,0, 0),
  162. array(-1, 1, 0),
  163. array(0, 0, 0));
  164. $this->_applyConvolutionMatrix($matrix);
  165. }
  166. public function edgeDetect() {
  167. $matrix = array(array(0,1, 0),
  168. array(1, -4, 1),
  169. array(0, 1, 0));
  170. $this->_applyConvolutionMatrix($matrix);
  171. }
  172. public function emboss() {
  173. $matrix = array(array(-2,-1, 0),
  174. array(-1, 1, 1),
  175. array(0, 1, 2));
  176. $this->_applyConvolutionMatrix($matrix);
  177. }
  178. private function _applyConvolutionMatrix($matrix) {
  179. $divisor = array_sum(array_map('array_sum', $matrix));
  180. imageconvolution($this->im, $matrix, $divisor, 0);
  181. }
  182. // code from http://www.ultramegatech.com/blog/2008/12/creating-a-captcha-php/
  183. public function creteCaptcha($code,$width, $height,$font='mitra.ttf') {
  184. // colors
  185. $r = mt_rand(160, 255);
  186. $g = mt_rand(160, 255);
  187. $b = mt_rand(160, 255);
  188. // create handle for new image
  189. $this->im = imagecreatetruecolor($width, $height);
  190. $this->width = $width;
  191. $this->height = $height;
  192. // create color handles
  193. $background = imagecolorallocate($this->im, $r, $g, $b);
  194. $text = imagecolorallocate($this->im, $r-128, $g-128, $b-128);
  195. // fill the background
  196. imagefill($this->im, 0, 0, $background);
  197. for($i = 1; $i <= strlen($code); $i++){
  198. $counter = mt_rand(0, 1);
  199. if ($counter == 0){
  200. $angle = mt_rand(0, 30);
  201. }
  202. if ($counter == 1){
  203. $angle = mt_rand(330, 360);
  204. }
  205. // "arial.ttf" can be replaced by any TTF font file stored in the same directory as the script
  206. imagettftext($this->im, mt_rand(18, 22), $angle, ($i * 18)-8, mt_rand(20, 25), $text, $font, substr($code, ($i - 1), 1));
  207. }
  208. // draw a line through the text
  209. imageline($this->im, 0, mt_rand(5, $height-5), $width, mt_rand(5, $height-5), $text);
  210. // blur the image
  211. $this->gaussianBlur();
  212. // add a border for looks
  213. imagerectangle($this->im, 0, 0, $width - 1, $height - 1, $text);
  214. return true;
  215. }
  216. }