Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to apply a filter to an image using imagefilter() function in PHP?
The imagefilter() function is a built-in PHP function that applies various visual effects and filters to images. It's commonly used for image processing tasks like adjusting brightness, applying blur effects, or converting images to grayscale.
Syntax
bool imagefilter(resource $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4)
Parameters
imagefilter() accepts the following parameters −
$image − The image resource to apply the filter to.
$filtertype − The filter constant that specifies which filter to apply.
$arg1, $arg2, $arg3, $arg4 − Optional arguments that depend on the filter type.
Available Filter Constants
IMG_FILTER_NEGATE − Reverses all colors of the image.
IMG_FILTER_GRAYSCALE − Converts the image to grayscale.
IMG_FILTER_BRIGHTNESS − Adjusts brightness (arg1: -255 to 255).
IMG_FILTER_CONTRAST − Adjusts contrast (arg1: contrast level).
IMG_FILTER_COLORIZE − Applies color tint (arg1: red, arg2: green, arg3: blue, arg4: alpha).
IMG_FILTER_EDGEDETECT − Highlights edges in the image.
IMG_FILTER_GAUSSIAN_BLUR − Applies Gaussian blur effect.
IMG_FILTER_EMBOSS − Creates emboss effect.
IMG_FILTER_MEAN_REMOVAL − Creates sketchy effect by removing noise.
IMG_FILTER_SMOOTH − Smooths the image (arg1: smoothness level).
IMG_FILTER_PIXELATE − Creates pixelation effect (arg1: block size, arg2: advanced mode).
Return Value
Returns true on success or false on failure.
Example 1: Applying Colorize Filter
This example demonstrates how to apply a colorize filter to tint an image ?
<?php
// Create image from file
$img = imagecreatefromjpeg('/path/to/image.jpg');
// Apply colorize filter (red: 140, green: 0, blue: 140, alpha: 20)
imagefilter($img, IMG_FILTER_COLORIZE, 140, 0, 140, 20);
// Output as PNG
header('Content-type: image/png');
imagepng($img);
// Clean up memory
imagedestroy($img);
?>
Example 2: Applying Negate Filter
This example shows how to invert the colors of an image ?
<?php
// Create image from file
$img = imagecreatefromjpeg('/path/to/image.jpg');
// Apply negate filter
imagefilter($img, IMG_FILTER_NEGATE);
// Output as PNG
header('Content-type: image/png');
imagepng($img);
// Clean up memory
imagedestroy($img);
?>
Example 3: Brightness and Blur Effects
This example demonstrates multiple filter applications ?
<?php
// Create image from file
$img = imagecreatefromjpeg('/path/to/image.jpg');
// Increase brightness
imagefilter($img, IMG_FILTER_BRIGHTNESS, 50);
// Apply Gaussian blur
imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
// Convert to grayscale
imagefilter($img, IMG_FILTER_GRAYSCALE);
// Output image
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
Note: The GD extension must be installed and enabled in PHP to use imagefilter(). Most web hosting providers include GD by default.
Conclusion
The imagefilter() function provides a powerful way to apply various visual effects to images in PHP. You can combine multiple filters to create complex effects, and always remember to clean up image resources with imagedestroy() to prevent memory leaks.
