PHP | imagefilter() Function

The imagefilter() function is an inbuilt function in PHP which is used to apply an given filter on the image.

Syntax:

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

Parameters: This function accept six parameters as mentioned above and described below:

  • $image: It specifies the image to be worked upon.
  • $filtertype: It specifies the filter to be used which is an integer corresponding to one of IMG_FILTER constants
    List of all IMG_FILTER constants are given below:

    • IMG_FILTER_NEGATE (0): Reverses all colors of the image.
    • IMG_FILTER_GRAYSCALE (1): Converts the image into grayscale by changing the red, green and blue components to their weighted sum.
    • IMG_FILTER_BRIGHTNESS (2): Changes the brightness of the image. Use $arg1 to set the level of brightness. The range for the brightness is -255 to 255.
    • IMG_FILTER_CONTRAST (3): Changes the contrast of the image. Use $arg1 to set the level of contrast.
    • IMG_FILTER_COLORIZE (4): Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use $arg1, $arg2 and $arg3 in the form of red, green, blue and arg4 for the alpha channel. The range for each color is 0 to 255.
    • IMG_FILTER_EDGEDETECT (5): Uses edge detection to highlight the edges in the image.
    • IMG_FILTER_GAUSSIAN_BLUR (6): Applies Gaussian Blur to image.
    • IMG_FILTER_SELECTIVE_BLUR (7): Applies Selective Blur to image.
    • IMG_FILTER_EMBOSS (8): Applies Emboss to image.
    • IMG_FILTER_MEAN_REMOVAL (9): Removes noise from image and gives ‘sketchy’ effect.
    • IMG_FILTER_SMOOTH (10): Makes the image smoother. Use $arg1 to set the level of smoothness.
    • IMG_FILTER_PIXELATE (11): Applies pixelation effect to the image, use $arg1 to set the block size and $arg2 to set the pixelation effect mode.
    • IMG_FILTER_SCATTER (12): Applies scatter effect to the image, use $arg1 and $arg2 to define the effect strength and additionally $arg3 to only apply the on select pixel colors.
  • $arg1 (Optional): It specifies the first argument.
  • $arg2 (Optional): It specifies the second argument.
  • $arg3 (Optional): It specifies the third argument.
  • $arg4 (Optional): It specifies the fourth argument.

Return Value: This function returns TRUE on success or FALSE on failure.



Below given programs illustrate the imagefilter() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Load the png image
$im = imagecreatefrompng(
  
// Negative the image
imagefilter($im, IMG_FILTER_NEGATE);
  
// Show the output
header('Content-type: image/png');
imagepng($im);
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Load the png image
$im = imagecreatefrompng(
  
// Grayscale the image
imagefilter($im, IMG_FILTER_GRAYSCALE);
  
// Show the output
header('Content-type: image/png');
imagepng($im);
?>

chevron_right


Output:

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Load the png image
$im = imagecreatefrompng(
  
// Colorize the image
imagefilter($im, IMG_FILTER_COLORIZE, 140, 0, 140, 20);
  
// Show the output
header('Content-type: image/png');
imagepng($im);
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.imagefilter.php




My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.