
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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); ?>
Output
- Related Articles
- How to destroy an image in PHP using imagedestroy() function?
- How to ensure an image is a truecolor image using the imageistruecolor() function in PHP?
- How to crop an image automatically using imagecropauto() function in PHP?
- How to rotate an image with a given angle using imagerotate() function in PHP?
- How to crop an image to the given rectangle using imagecrop() function using PHP?
- How to apply a gamma correction to a Graphics Draw (GD) image in PHP?
- How to apply Perspective Transformations on an image using OpenCV Python?
- How to get or set the resolution of an image using imageresolution() function in PHP?
- How to apply Posterize to an image in Node Jimp?
- How to apply a Sepia tone to an image in Node Jimp?
- How to perform bilateral filter operation on an image in OpenCV using Python?
- How to draw a text string image horizontally by using imagestring() function in PHP?
- Applying rank filter to an image using the Pillow library
- How to set the tile image for filling using imagesettile() function in PHP?
- How to filter values from an array using the comparator function in JavaScript?
