Open In App

PHP | imagefilter() Function

Last Updated : 14 Feb, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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: php
<?php

// Load the png image
$im = imagecreatefrompng(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

// Negative the image
imagefilter($im, IMG_FILTER_NEGATE);

// Show the output
header('Content-type: image/png');
imagepng($im);
?>
Output: Program 2: php
<?php

// Load the png image
$im = imagecreatefrompng(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

// Grayscale the image
imagefilter($im, IMG_FILTER_GRAYSCALE);

// Show the output
header('Content-type: image/png');
imagepng($im);
?>
Output: Program 3: php
<?php

// Load the png image
$im = imagecreatefrompng(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

// Colorize the image
imagefilter($im, IMG_FILTER_COLORIZE, 140, 0, 140, 20);

// Show the output
header('Content-type: image/png');
imagepng($im);
?>
Output: Reference: https://www.php.net/manual/en/function.imagefilter.php

Next Article

Similar Reads