(PHP 5)

imagefilter — Applies a filter to an image


bool imagefilter ( resource $image , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3[, int $arg4 ]]]] )

imagefilter() applies the given filter filtertype on the image.


An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
filtertype can be one of the following:

  • IMG_FILTER_NEGATE: Reverses all colors of the image.
  • IMG_FILTER_GRAYSCALE: Converts the image into grayscale.
  • IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. Use arg1 to set the level of brightness.
  • IMG_FILTER_CONTRAST: Changes the contrast of the image. Use arg1 to set the level of contrast.
  • IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use arg1arg2 andarg3 in the form of redgreenblue and arg4 for the alpha channel. The range for each color is 0 to 255.
  • IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.
  • IMG_FILTER_EMBOSS: Embosses the image.
  • IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method.
  • IMG_FILTER_SELECTIVE_BLUR: Blurs the image.
  • IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a “sketchy” effect.
  • IMG_FILTER_SMOOTH: Makes the image smoother. Use arg1 to set the level of smoothness.
  • IMG_FILTER_PIXELATE: Applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode.
  • IMG_FILTER_BRIGHTNESS: Brightness level.
  • IMG_FILTER_CONTRAST: Contrast level.
  • IMG_FILTER_COLORIZE: Value of red component.
  • IMG_FILTER_SMOOTH: Smoothness level.
  • IMG_FILTER_PIXELATE: Block size in pixels.
  • IMG_FILTER_COLORIZE: Value of green component.
  • IMG_FILTER_PIXELATE: Whether to use advanced pixelation effect or not (defaults to FALSE).
  • IMG_FILTER_COLORIZE: Value of blue component.
  • IMG_FILTER_COLORIZE: Alpha channel, A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.

Return Values

Returns TRUE on success or FALSE on failure.


imagefilter($dst_r, IMG_FILTER_BRIGHTNESS, -80);

The documentation misses the exact meaning and valid ranges of the arguments for ImageFilter(). According to the 5.2.0 sources the arguments are:
-255 = min brightness, 0 = no change, +255 = max brightness

-100 = max contrast, 0 = no change, +100 = min contrast (note the direction!)

Adds (subtracts) specified RGB values to each pixel. The valid range for each color is -255…+255, not 0…255. The correct order is red, green, blue.
-255 = min, 0 = no change, +255 = max
This has not much to do with IMG_FILTER_GRAYSCALE.

Applies a 9-cell convolution matrix where center pixel has the weight arg1 and others weight of 1.0. The result is normalized by dividing the sum with arg1 + 8.0 (sum of the matrix).
any float is accepted, large value (in practice: 2048 or more) = no change

ImageFilter seem to return false if the argument(s) are out of range for the chosen filter.



You must add imagesavealpha($im, true); so the alpha channel will be saved on the new image.

$im = imagecreatefrompng('image.png');
imagealphablending($im, false);

imagesavealpha($im, true);

if($im && imagefilter($im, IMG_FILTER_COLORIZE, 0,0,255,0)) {
    imagepng($im, 'image-new.png');