imagefilter.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. //include this file whenever you have to use imageconvolution…
  3. //you can use in your project, but keep the comment below
  4. //great for any image manipulation library
  5. //Made by Chao Xu(Mgccl) 3/1/07
  6. //www.webdevlogs.com
  7. //V 1.0
  8. if (!function_exists('imagefilter')) {
  9. function imagefilter($source, $var, $arg1 = null, $arg2 = null, $arg3 = null) {
  10. #define(’IMAGE_FILTER_NEGATE’, 0);
  11. #define(’IMAGE_FILTER_GRAYSCALE’, 1);
  12. #define(’IMAGE_FILTER_BRIGHTNESS’, 2);
  13. #define(’IMAGE_FILTER_CONTRAST’, 3);
  14. #define(’IMAGE_FILTER_COLORIZE’, 4);
  15. #define(’IMAGE_FILTER_EDGEDETECT’, 5);
  16. #define(’IMAGE_FILTER_EMBOSS’, 6);
  17. #define(’IMAGE_FILTER_GAUSSIAN_BLUR’, 7);
  18. #define(’IMAGE_FILTER_SELECTIVE_BLUR’, 8);
  19. #define(’IMAGE_FILTER_MEAN_REMOVAL’, 9);
  20. #define(’IMAGE_FILTER_SMOOTH’, 10);
  21. $max_y = imagesy($source);
  22. $max_x = imagesx($source);
  23. switch ($var) {
  24. case 0:
  25. $y = 0;
  26. while ($y<$max_y) {
  27. $x = 0;
  28. while ($x<$max_x) {
  29. $rgb = imagecolorat($source, $x, $y);
  30. $r = 255 - (($rgb >> 16) & 0xFF);
  31. $g = 255 - (($rgb >> 8) & 0xFF);
  32. $b = 255 - ($rgb & 0xFF);
  33. $a = $rgb >> 24;
  34. $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
  35. if ($new_pxl == false) {
  36. $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
  37. }
  38. imagesetpixel($source, $x, $y, $new_pxl);
  39. ++$x;
  40. }
  41. ++$y;
  42. }
  43. return true;
  44. break;
  45. case 1:
  46. $y = 0;
  47. while ($y<$max_y) {
  48. $x = 0;
  49. while ($x<$max_x) {
  50. $rgb = imagecolorat($source, $x, $y);
  51. $a = $rgb >> 24;
  52. $r = ((($rgb >> 16) & 0xFF)*0.299)+((($rgb >> 8) & 0xFF)*0.587)+(($rgb & 0xFF)*0.114);
  53. $new_pxl = imagecolorallocatealpha($source, $r, $r, $r, $a);
  54. if ($new_pxl == false) {
  55. $new_pxl = imagecolorclosestalpha($source, $r, $r, $r, $a);
  56. }
  57. imagesetpixel($source, $x, $y, $new_pxl);
  58. ++$x;
  59. }
  60. ++$y;
  61. }
  62. return true;
  63. break;
  64. case 2:
  65. $y = 0;
  66. while ($y<$max_y) {
  67. $x = 0;
  68. while ($x<$max_x) {
  69. $rgb = imagecolorat($source, $x, $y);
  70. $r = (($rgb >> 16) & 0xFF) + $arg1;
  71. $g = (($rgb >> 8) & 0xFF) + $arg1;
  72. $b = ($rgb & 0xFF) + $arg1;
  73. $a = $rgb >> 24;
  74. $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
  75. $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
  76. $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
  77. $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
  78. if ($new_pxl == false) {
  79. $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
  80. }
  81. imagesetpixel($source, $x, $y, $new_pxl);
  82. ++$x;
  83. }
  84. ++$y;
  85. }
  86. return true;
  87. break;
  88. case 3:
  89. $contrast = pow((100-$arg1)/100, 2);
  90. $y = 0;
  91. while ($y<$max_y) {
  92. $x = 0;
  93. while ($x<$max_x) {
  94. $rgb = imagecolorat($source, $x, $y);
  95. $a = $rgb >> 24;
  96. $r = (((((($rgb >> 16) & 0xFF)/255)-0.5)*$contrast)+0.5)*255;
  97. $g = (((((($rgb >> 8) & 0xFF)/255)-0.5)*$contrast)+0.5)*255;
  98. $b = ((((($rgb & 0xFF)/255)-0.5)*$contrast)+0.5)*255;
  99. $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
  100. $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
  101. $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
  102. $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
  103. if ($new_pxl == false) {
  104. $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
  105. }
  106. imagesetpixel($source, $x, $y, $new_pxl);
  107. ++$x;
  108. }
  109. ++$y;
  110. }
  111. return true;
  112. break;
  113. case 4:
  114. $x = 0;
  115. while ($x<$max_x) {
  116. $y = 0;
  117. while ($y<$max_y) {
  118. $rgb = imagecolorat($source, $x, $y);
  119. $r = (($rgb >> 16) & 0xFF) + $arg1;
  120. $g = (($rgb >> 8) & 0xFF) + $arg2;
  121. $b = ($rgb & 0xFF) + $arg3;
  122. $a = $rgb >> 24;
  123. $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
  124. $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
  125. $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
  126. $new_pxl = imagecolorallocatealpha($source, $r, $g, $b, $a);
  127. if ($new_pxl == false) {
  128. $new_pxl = imagecolorclosestalpha($source, $r, $g, $b, $a);
  129. }
  130. imagesetpixel($source, $x, $y, $new_pxl);
  131. ++$y;
  132. }
  133. ++$x;
  134. }
  135. return true;
  136. break;
  137. case 5:
  138. return imageconvolution($source, array(array(-1, 0, -1), array(0, 4, 0), array(-1, 0, -1)), 1, 127);
  139. break;
  140. case 6:
  141. return imageconvolution($source, array(array(1.5, 0, 0), array(0, 0, 0), array(0, 0, -1.5)), 1, 127);
  142. break;
  143. case 7:
  144. return imageconvolution($source, array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)), 16, 0);
  145. break;
  146. case 8:
  147. for ($y = 0; $y<$max_y; $y++) {
  148. for ($x = 0; $x<$max_x; $x++) {
  149. $flt_r_sum = $flt_g_sum = $flt_b_sum = 0;
  150. $cpxl = imagecolorat($source, $x, $y);
  151. for ($j=0; $j<3; $j++) {
  152. for ($i=0; $i<3; $i++) {
  153. if (($j == 1) && ($i == 1)) {
  154. $flt_r[1][1] = $flt_g[1][1] = $flt_b[1][1] = 0.5;
  155. }
  156. else {
  157. $pxl = imagecolorat($source, $x-(3>>1)+$i, $y-(3>>1)+$j);
  158. $new_a = $pxl >> 24;
  159. //$r = (($pxl >> 16) & 0xFF);
  160. //$g = (($pxl >> 8) & 0xFF);
  161. //$b = ($pxl & 0xFF);
  162. $new_r = abs((($cpxl >> 16) & 0xFF) - (($pxl >> 16) & 0xFF));
  163. if ($new_r != 0) {
  164. $flt_r[$j][$i] = 1/$new_r;
  165. }
  166. else {
  167. $flt_r[$j][$i] = 1;
  168. }
  169. $new_g = abs((($cpxl >> 8) & 0xFF) - (($pxl >> 8) & 0xFF));
  170. if ($new_g != 0) {
  171. $flt_g[$j][$i] = 1/$new_g;
  172. }
  173. else {
  174. $flt_g[$j][$i] = 1;
  175. }
  176. $new_b = abs(($cpxl & 0xFF) - ($pxl & 0xFF));
  177. if ($new_b != 0) {
  178. $flt_b[$j][$i] = 1/$new_b;
  179. }
  180. else {
  181. $flt_b[$j][$i] = 1;
  182. }
  183. }
  184. $flt_r_sum += $flt_r[$j][$i];
  185. $flt_g_sum += $flt_g[$j][$i];
  186. $flt_b_sum += $flt_b[$j][$i];
  187. }
  188. }
  189. for ($j=0; $j<3; $j++) {
  190. for ($i=0; $i<3; $i++) {
  191. if ($flt_r_sum != 0) {
  192. $flt_r[$j][$i] /= $flt_r_sum;
  193. }
  194. if ($flt_g_sum != 0) {
  195. $flt_g[$j][$i] /= $flt_g_sum;
  196. }
  197. if ($flt_b_sum != 0) {
  198. $flt_b[$j][$i] /= $flt_b_sum;
  199. }
  200. }
  201. }
  202. $new_r = $new_g = $new_b = 0;
  203. for ($j=0; $j<3; $j++) {
  204. for ($i=0; $i<3; $i++) {
  205. $pxl = imagecolorat($source, $x-(3>>1)+$i, $y-(3>>1)+$j);
  206. $new_r += (($pxl >> 16) & 0xFF) * $flt_r[$j][$i];
  207. $new_g += (($pxl >> 8) & 0xFF) * $flt_g[$j][$i];
  208. $new_b += ($pxl & 0xFF) * $flt_b[$j][$i];
  209. }
  210. }
  211. $new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
  212. $new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
  213. $new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);
  214. $new_pxl = imagecolorallocatealpha($source, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
  215. if ($new_pxl == false) {
  216. $new_pxl = imagecolorclosestalpha($source, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
  217. }
  218. imagesetpixel($source, $x, $y, $new_pxl);
  219. }
  220. }
  221. return true;
  222. break;
  223. case 9:
  224. return imageconvolution($source, array(array(-1, -1, -1), array(-1, 9, -1), array(-1, -1, -1)), 1, 0);
  225. break;
  226. case 10:
  227. return imageconvolution($source, array(array(1, 1, 1), array(1, $arg1, 1), array(1, 1, 1)), $arg1+8, 0);
  228. break;
  229. }
  230. }
  231. }