You are here

function imagefilter in ImageAPI 5

Same name and namespace in other branches
  1. 6 imagefilter.inc \imagefilter()
2 string references to 'imagefilter'
imageapi_gd_image_desaturate in ./imageapi_gd.module
Convert an image resource to grayscale.
imageapi_gd_requirements in ./imageapi_gd.install

File

./imagefilter.inc, line 87

Code

function imagefilter(&$im, $var, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  switch ($var) {
    case IMG_FILTER_NEGATE:
      _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_negate');
      return TRUE;
    case IMG_FILTER_GRAYSCALE:
      _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_grayscale');
      return TRUE;
    case IMG_FILTER_BRIGHTNESS:
      _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_brightness', $arg1);
      return TRUE;
    case IMG_FILTER_CONTRAST:

      // normalize between 0-1, square to keep positive.
      $contrast = pow((100 - $arg1) / 100, 2);
      _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_contrast', $contrast);
      return TRUE;
    case IMG_FILTER_COLORIZE:
      $arg1 = is_null($arg1) ? 0 : $arg1;
      $arg2 = is_null($arg2) ? 0 : $arg2;
      $arg3 = is_null($arg3) ? 0 : $arg3;
      $arg4 = is_null($arg4) ? 0 : $arg4;
      _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_colorize', $arg1, $arg2, $arg3, $arg4);
      return TRUE;
    case IMG_FILTER_EDGEDETECT:
      return imageconvolution($im, array(
        array(
          -1,
          0,
          -1,
        ),
        array(
          0,
          4,
          0,
        ),
        array(
          -1,
          0,
          -1,
        ),
      ), 1, 127);
    case IMG_FILTER_EMBOSS:
      return imageconvolution($im, array(
        array(
          1.5,
          0,
          0,
        ),
        array(
          0,
          0,
          0,
        ),
        array(
          0,
          0,
          -1.5,
        ),
      ), 1, 127);
    case IMG_FILTER_GAUSSIAN_BLUR:
      return imageconvolution($im, array(
        array(
          1,
          2,
          1,
        ),
        array(
          2,
          4,
          2,
        ),
        array(
          1,
          2,
          1,
        ),
      ), 16, 0);
    case IMG_FILTER_SELECTIVE_BLUR:
      for ($y = 0; $y < $max_y; ++$y) {
        for ($x = 0; $x < $max_x; ++$x) {
          $flt_r_sum = $flt_g_sum = $flt_b_sum = 0;
          $cpxl = imagecolorat($im, $x, $y);
          for ($j = 0; $j < 3; ++$j) {
            for ($i = 0; $i < 3; ++$i) {
              if ($j == 1 && $i == 1) {
                $flt_r[1][1] = $flt_g[1][1] = $flt_b[1][1] = 0.5;
              }
              else {
                $pxl = imagecolorat($im, $x - (3 >> 1) + $i, $y - (3 >> 1) + $j);
                $new_a = $pxl >> 24;

                //$r = (($pxl >> 16) & 0xFF);

                //$g = (($pxl >> 8) & 0xFF);

                //$b = ($pxl & 0xFF);
                $new_r = abs(($cpxl >> 16 & 0xff) - ($pxl >> 16 & 0xff));
                if ($new_r != 0) {
                  $flt_r[$j][$i] = 1 / $new_r;
                }
                else {
                  $flt_r[$j][$i] = 1;
                }
                $new_g = abs(($cpxl >> 8 & 0xff) - ($pxl >> 8 & 0xff));
                if ($new_g != 0) {
                  $flt_g[$j][$i] = 1 / $new_g;
                }
                else {
                  $flt_g[$j][$i] = 1;
                }
                $new_b = abs(($cpxl & 0xff) - ($pxl & 0xff));
                if ($new_b != 0) {
                  $flt_b[$j][$i] = 1 / $new_b;
                }
                else {
                  $flt_b[$j][$i] = 1;
                }
              }
              $flt_r_sum += $flt_r[$j][$i];
              $flt_g_sum += $flt_g[$j][$i];
              $flt_b_sum += $flt_b[$j][$i];
            }
          }
          for ($j = 0; $j < 3; ++$j) {
            for ($i = 0; $i < 3; ++$i) {
              if ($flt_r_sum != 0) {
                $flt_r[$j][$i] /= $flt_r_sum;
              }
              if ($flt_g_sum != 0) {
                $flt_g[$j][$i] /= $flt_g_sum;
              }
              if ($flt_b_sum != 0) {
                $flt_b[$j][$i] /= $flt_b_sum;
              }
              $new_r = $new_g = $new_b = 0;
              for ($j = 0; $j < 3; ++$j) {
                for ($i = 0; $i < 3; ++$i) {
                  $pxl = imagecolorat($im, $x - (3 >> 1) + $i, $y - (3 >> 1) + $j);
                  $new_r += ($pxl >> 16 & 0xff) * $flt_r[$j][$i];
                  $new_g += ($pxl >> 8 & 0xff) * $flt_g[$j][$i];
                  $new_b += ($pxl & 0xff) * $flt_b[$j][$i];
                }
              }
              $new_r = $new_r > 255 ? 255 : ($new_r < 0 ? 0 : $new_r);
              $new_g = $new_g > 255 ? 255 : ($new_g < 0 ? 0 : $new_g);
              $new_b = $new_b > 255 ? 255 : ($new_b < 0 ? 0 : $new_b);
              $new_pxl = imagecolorallocatealpha($im, (int) $new_r, (int) $new_g, (int) $new_b, $new_a);
              if ($new_pxl == FALSE) {
                $new_pxl = imagecolorclosestalpha($im, (int) $new_r, (int) $new_g, (int) $new_b, $new_a);
              }
              imagesetpixel($im, $x, $y, $new_pxl);
            }
          }
        }
      }
      return TRUE;
    case IMG_FILTER_MEAN_REMOVAL:
      return imageconvolution($im, array(
        array(
          -1,
          -1,
          -1,
        ),
        array(
          -1,
          9,
          -1,
        ),
        array(
          -1,
          -1,
          -1,
        ),
      ), 1, 0);
    case IMG_FILTER_SMOOTH:
      return imageconvolution($im, array(
        array(
          1,
          1,
          1,
        ),
        array(
          1,
          $arg1,
          1,
        ),
        array(
          1,
          1,
          1,
        ),
      ), $arg1 + 8, 0);
  }
}