imagefilter() is a built-in function in PHP that is used to apply a given filter to an image.
bool imagefilter(resource $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4)
imagefilter() Takes six different parameters - $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4.
$image - It holds image resources.
< span>$filtertype - Specifies the filter to use, which is an integer.
Below are the different image filter constants given - p>
##IMG_FILTER_NEGATE - Inverts all colors of the image.
IMG_FILTER_GRAYSCALE - Converts the image to grayscale by changing the red, green and blue components to their weighted sum.
IMG_FILTER_BRIGHTNESS - Change the brightness of the image. arg1 is used to set the brightness level. Brightness range is -255 to 255.
IMG_FILTER_CONSTRAST - Changes the contrast of the image. $arg1 is used to set the contrast level.
IMG_FILTER_COLORIZE - This image filter is similar to IMG_FILTER_GARYSCALE except we can specify the color, it uses parameters arg1, arg2 and $arg3 , in the form of red, green, blue, arg4 is used for the Alpha channel. Each color ranges from 0 to 255.
IMG_FILTER_EDGEDETECT - This filter is used for edge detection to highlight edges in an image. < /p>
IMG_FILTER_GAUSSIAN_BLUR - Applies a Gaussian blur to the image.
IMG_FILTER_SELECTIVE_BLUR > - Applies a selective blur to the image.
IMG_FILTER_EMBOSS - Applies an embossing to the image.
IMG_FILTER_SMOOTH - Makes the image smoother. $arg1 is used to set the smoothness.
IMG_FILTER_PIXELATE - Apply pixelation effect to the image. $arg1 is used to set the block size, $arg2 is used to set the pixelation effect mode. p>
IMG_FILTR_SCATTER - Applies a scattering effect to the image. $arg1 and arg2 are used to define the effect strength, $arg3 is used to define the effect strength for application to the selected pixel color.
arg1
IMG_FILTER_BRIGHTNESS - Use at the brightness level.
IMG_FILT_CONTRAST - Value used for contrast
IMG_FILTER_COLORIZE - Value used for the red component.
IMG_FILTER_SMOOTH - for smoothness.
IMG_FILTER_PIXELATE - For block size in pixels.
IMG_FILTER_SCATTER - Used for effect deduction levels.
arg2
IMG_FILTER_COLORIZE - Value to use for the blue component.
IMG_FILTER_PIXELATE - Whether to use advanced pixelation effect (default is false).
IMG_FILTER_SCATTER - Affects the added level. >
arg3
IMG_FILTER_COLORIZE - Use the value of the blue component.
IMG_FILTER_SCATTER - Optional array of indexed color values used to apply the effect.
arg4
IMG_FILTER_COLORIZE - Alpha channel, value between 0 and 127 . 0 means fully opaque, 127 means fully transparent.
<?php // Load the gif image from the local drive folder. $img = imagecreatefromgif('C:\xampp\htdocs\Images\img39.gif'); // Colorize the image imagefilter($img, IMG_FILTER_COLORIZE, 140, 0, 140, 20); // Show the output image header('Content-type: image/gif'); imagepng($img); ?>
Example 2
<?php // Load the gif image from the local drive folder. $img = imagecreatefromgif('C:\xampp\htdocs\Images\img39.gif'); // Negative the image imagefilter($img, IMG_FILTER_NEGATE); // Show the output image header('Content-type: image/gif'); imagepng($img); ?>
The above is the detailed content of How to apply a filter to an image using the imagefilter() function in PHP?. For more information, please follow other related articles on the PHP Chinese website!