Advertisement
krot

IMG_FILTER

Nov 19th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.03 KB | None | 0 0
  1. <?php
  2.  
  3. //Made by Chao Xu(Mgccl) 3/1/07
  4. //www.webdevlogs.com
  5. //V 1.0
  6.  
  7. // Drupal code style and some restructuring by dopry. http://www.darrelopry.com
  8.  
  9.  
  10. define('IMG_FILTER_NEGATE', 0);
  11. define('IMG_FILTER_GRAYSCALE', 1);
  12. define('IMG_FILTER_BRIGHTNESS', 2);
  13. define('IMG_FILTER_CONTRAST', 3);
  14. define('IMG_FILTER_COLORIZE', 4);
  15. define('IMG_FILTER_EDGEDETECT', 5);
  16. define('IMG_FILTER_EMBOSS', 6);
  17. define('IMG_FILTER_GAUSSIAN_BLUR', 7);
  18. define('IMG_FILTER_SELECTIVE_BLUR', 8);
  19. define('IMG_FILTER_MEAN_REMOVAL', 9);
  20. define('IMG_FILTER_SMOOTH', 10);
  21.  
  22. define('IMAGEAPI_IMAGEFILTER_PHP', 1);
  23. /**
  24.  * walk each pixel in an image applying $callback to it.
  25.  */
  26. function _imageapi_gd_pixel_color_walk(&$im, $callback, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  27.   $max_y = imagesy($im);
  28.   $max_x = imagesx($im);
  29.   for ($y=0; $y < $max_y; ++$y) {
  30.     for ($x=0; $x < $max_x; ++$x) {
  31.       $rgb = imagecolorat($im, $x, $y);
  32.       $r = ($rgb >> 16) & 0xFF;
  33.       $g = ($rgb >> 8) & 0xFF;
  34.       $b = ($rgb & 0xFF);
  35.       $a = $rgb >> 24;
  36.  
  37.       $callback($r, $g, $b, $a, $arg1, $arg2, $arg3, $arg4);
  38.  
  39.       // sanitize rgb values.
  40.       $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
  41.       $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
  42.       $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
  43.       $a = ($a > 127)? 127 : (($a < 0)? 0:$a);
  44.  
  45.       if (!$color = imagecolorallocatealpha($im, $r, $g, $b, $a)) {
  46.         $color = imagecolorclosestalpha($im, $r, $g, $b, $a);
  47.       }
  48.       imagesetpixel($im, $x, $y, $color);
  49.     }
  50.   }
  51. }
  52.  
  53. function _imageapi_gd_pixel_negate(&$r, &$g, &$b, &$a) {
  54.   $r = 255 - $r;
  55.   $g = 255 - $g;
  56.   $b = 255 - $b;
  57. }
  58.  
  59.  
  60. function _imageapi_gd_pixel_grayscale(&$r, &$g, &$b, &$a) {
  61.   $r = round($r * 0.299 + $g * 0.587 + $b * 0.114);
  62.   $g = $r;
  63.   $b = $r;
  64. }
  65.  
  66. function _imageapi_gd_pixel_brightness(&$r, &$g, &$b, &$a, $arg1) {
  67.   $r += $arg1;
  68.   $g += $arg1;
  69.   $b += $arg1;
  70. }
  71.  
  72. function _imageapi_gd_pixel_contrast(&$r, &$g, &$b, &$a, $contrast) {
  73.   // normalize color value between -0.5 - 0.5
  74.   // multiply by contrast value to accentuate positive/negative value.
  75.   // denormalize to 0-255 range.
  76.   $r = ((($r/255 - 0.5) * $contrast) + 0.5) * 255;
  77.   $g = ((($g/255 - 0.5) * $contrast) + 0.5) * 255;
  78.   $b = ((($b/255 - 0.5) * $contrast) + 0.5) * 255;
  79. }
  80.  
  81. function _imageapi_gd_pixel_colorize(&$r, &$g, &$b, &$a, $arg1, $arg2, $arg3, $arg4) {
  82.   $r += $arg1;
  83.   $g += $arg2;
  84.   $b += $arg3;
  85. }
  86.  
  87. function imagefilter(&$im, $var, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  88.  
  89.   switch ($var) {
  90.     case IMG_FILTER_NEGATE:
  91.       _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_negate');
  92.       return TRUE;
  93.  
  94.     case IMG_FILTER_GRAYSCALE:
  95.       _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_grayscale');
  96.       return TRUE;
  97.  
  98.     case IMG_FILTER_BRIGHTNESS:
  99.       _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_brightness', $arg1);
  100.       return TRUE;
  101.  
  102.     case IMG_FILTER_CONTRAST:
  103.       // normalize between 0-1, square to keep positive.
  104.       $contrast = pow((100-$arg1)/100, 2);
  105.       _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_contrast', $contrast);
  106.       return TRUE;
  107.  
  108.     case IMG_FILTER_COLORIZE:
  109.       $arg1 = (is_null($arg1)) ? 0 : $arg1;
  110.       $arg2 = (is_null($arg2)) ? 0 : $arg2;
  111.       $arg3 = (is_null($arg3)) ? 0 : $arg3;
  112.       $arg4 = (is_null($arg4)) ? 0 : $arg4;
  113.       _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_colorize', $arg1, $arg2, $arg3, $arg4);
  114.       return TRUE;
  115.  
  116.     case IMG_FILTER_EDGEDETECT:
  117.       return imageconvolution($im, array(array(-1, 0, -1), array(0, 4, 0), array(-1, 0, -1)), 1, 127);
  118.  
  119.     case IMG_FILTER_EMBOSS:
  120.       return imageconvolution($im, array(array(1.5, 0, 0), array(0, 0, 0), array(0, 0, -1.5)), 1, 127);
  121.  
  122.     case IMG_FILTER_GAUSSIAN_BLUR:
  123.       return imageconvolution($im, array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)), 16, 0);
  124.  
  125.     case IMG_FILTER_SELECTIVE_BLUR:
  126.       for ($y = 0; $y<$max_y; ++$y) {
  127.         for ($x = 0; $x<$max_x; ++$x) {
  128.           $flt_r_sum = $flt_g_sum = $flt_b_sum = 0;
  129.           $cpxl = imagecolorat($im, $x, $y);
  130.           for ($j=0; $j<3; ++$j) {
  131.             for ($i=0; $i<3; ++$i) {
  132.               if (($j == 1) && ($i == 1)) {
  133.                 $flt_r[1][1] = $flt_g[1][1] = $flt_b[1][1] = 0.5;
  134.               }
  135.               else {
  136.                 $pxl = imagecolorat($im, $x-(3>>1)+$i, $y-(3>>1)+$j);
  137.                 $new_a = $pxl >> 24;
  138.                 //$r = (($pxl >> 16) & 0xFF);
  139.                 //$g = (($pxl >> 8) & 0xFF);
  140.                 //$b = ($pxl & 0xFF);
  141.                 $new_r = abs((($cpxl >> 16) & 0xFF) - (($pxl >> 16) & 0xFF));
  142.                 if ($new_r != 0) {
  143.                   $flt_r[$j][$i] = 1/$new_r;
  144.                 }
  145.                 else {
  146.                   $flt_r[$j][$i] = 1;
  147.                 }
  148.  
  149.                 $new_g = abs((($cpxl >> 8) & 0xFF) - (($pxl >> 8) & 0xFF));
  150.                 if ($new_g != 0) {
  151.                   $flt_g[$j][$i] = 1/$new_g;
  152.                 }
  153.                 else {
  154.                   $flt_g[$j][$i] = 1;
  155.                 }
  156.  
  157.                 $new_b = abs(($cpxl & 0xFF) - ($pxl & 0xFF));
  158.                 if ($new_b != 0) {
  159.                   $flt_b[$j][$i] = 1/$new_b;
  160.                 }
  161.                 else {
  162.                   $flt_b[$j][$i] = 1;
  163.                 }
  164.               }
  165.  
  166.               $flt_r_sum += $flt_r[$j][$i];
  167.               $flt_g_sum += $flt_g[$j][$i];
  168.               $flt_b_sum += $flt_b[$j][$i];
  169.             }
  170.           }
  171.  
  172.           for ($j=0; $j<3; ++$j) {
  173.             for ($i=0; $i<3; ++$i) {
  174.               if ($flt_r_sum != 0) $flt_r[$j][$i] /= $flt_r_sum;
  175.               if ($flt_g_sum != 0) $flt_g[$j][$i] /= $flt_g_sum;
  176.               if ($flt_b_sum != 0) $flt_b[$j][$i] /= $flt_b_sum;
  177.  
  178.               $new_r = $new_g = $new_b = 0;
  179.  
  180.               for ($j=0; $j<3; ++$j) {
  181.                 for ($i=0; $i<3; ++$i) {
  182.                   $pxl = imagecolorat($im, $x-(3>>1)+$i, $y-(3>>1)+$j);
  183.                   $new_r += (($pxl >> 16) & 0xFF) * $flt_r[$j][$i];
  184.                   $new_g += (($pxl >> 8) & 0xFF) * $flt_g[$j][$i];
  185.                   $new_b += ($pxl & 0xFF) * $flt_b[$j][$i];
  186.                 }
  187.               }
  188.  
  189.               $new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
  190.               $new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
  191.               $new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);
  192.               $new_pxl = imagecolorallocatealpha($im, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
  193.               if ($new_pxl == FALSE) {
  194.                 $new_pxl = imagecolorclosestalpha($im, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
  195.               }
  196.               imagesetpixel($im, $x, $y, $new_pxl);
  197.             }
  198.           }
  199.         }
  200.       }
  201.       return TRUE;
  202.  
  203.     case IMG_FILTER_MEAN_REMOVAL:
  204.       return imageconvolution($im, array(array(-1, -1, -1), array(-1, 9, -1), array(-1, -1, -1)), 1, 0);
  205.  
  206.     case IMG_FILTER_SMOOTH:
  207.       return imageconvolution($im, array(array(1, 1, 1), array(1, $arg1, 1), array(1, 1, 1)), $arg1 + 8, 0);
  208.   }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement