- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to apply a filter to an image using imagefilter() function in PHP?
imagefilter() is an inbuilt function in PHP that is used to apply a given filter to an image.
Syntax
bool imagefilter(resource $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4)
Parameters
imagefilter() takes six different parameters − $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4.
$image − It holds the image resource.
$filtertype − Specifies the filter to be used which is an integer.
Below are the given different image filter constants −
IMG_FILTER_NEGATE − Reverses all the colors of an image.
IMG_FILTER_GRAYSCALE − Converts the image into the grayscale by changing the red, green, and blue components to their weighted sum.
IMG_FILTER_BRIGHTNESS − Changes the brightness of the image. arg1 is used to set the level of brightness. The range for the brightness is -255 to 255.
IMG_FILTER_CONSTRAST − Changes the contrast of the image. $arg1 is used to set the level of contrast.
IMG_FILTER_COLORIZE − This image filter is like the IMG_FILTER_GARYSCALE, except that we can specify the color, it uses the arguments arg1, arg2, and $arg3 in the form of red, green, blue and the arg4 is used for the alpha channel. The range for each color is from 0 to 255.
IMG_FILTER_EDGEDETECT − This filter is used for edge detection to highlight the edges in the image.
IMG_FILTER_GAUSSIAN_BLUR − Apply Gaussian blur to the image.
IMG_FILTER_SELECTIVE_BLUR − Apply selective blur to the image.
IMG_FILTER_EMBOSS − Apply Emboss to the image.
IMG_FILTER_MEAN_REMOVAL − Remove noise from the image and provide sketchy effect.
IMG_FILTER_SMOOTH − Makes the image smoother. $arg1 is used to set the level of smoothness.
IMG_FILTER_PIXELATE − Apply pixelation effect to the image. $arg1 is used to set the block size, and $arg2 to set the pixelation effect mode.
IMG_FILTR_SCATTER − Apply scatter effect to the image. $arg1 and arg2 are used to define the effect strength and $arg3 is used to apply on select pixel colors.
List of optional arguments
arg1
IMG_FILTER_BRIGHTNESS − Used for brightness level.
IMG_FILT_CONTRAST − Used for contrast level.
IMG_FILTER_COLORIZE − Used for the value of the red component.
IMG_FILTER_SMOOTH − Used for smoothness level.
IMG_FILTER_PIXELATE − Used for block size in pixels.
IMG_FILTER_SCATTER − Used for effect subtraction level.
arg2
IMG_FILTER_COLORIZE − It is used for the value of the blue component.
IMG_FILTER_PIXELATE − Whether to use advanced pixelation effect or not (defaults to false).
IMG_FILTER_SCATTER − Effect the addition level.
arg3
IMG_FILTER_COLORIZE − It is used for the value of the blue component.
IMG_FILTER_SCATTER − Optional array indexed color values to apply effect at.
arg4
IMG_FILTER_COLORIZE − Alpha channel, A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
Return Values
It returns True on success and False on failure.
Example 1
<?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); ?>
Output
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); ?>