WEBINAR:
On-Demand
Application Security Testing: An Integral Part of DevOps
Predefined filters in PHP
PHP (through the GD library) includes a reasonably-sized set of predefined filters, some of which provide the same functionality as filters you've already seen. You use the
imagefilter function to apply different image filters as follows:
bool imagefilter(resource $image, int $filtertype
[, int $arg1 [, int $arg2 [, int $arg3
[, int $arg4 ]]]])
The
imagefilter function applies the given filter to the given image. The
$filtertype argument can be one of the following values (taken from the official PHP manual). The optional arguments
$arg1,
$arg2 and so forth apply only to some of the filters:
- IMG_FILTER_NEGATE: Reverses all colors of the image.
- IMG_FILTER_GRAYSCALE: Converts the image into grayscale.
- IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. Use arg1 to set the level of brightness.
- IMG_FILTER_CONTRAST: Changes the contrast of the image. Use arg1 to set the level of contrast.
- IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except that you can specify the color. Use arg1, arg2 and arg3 as the red, blue, and green values, and arg4 for the alpha channel. The range for each is 0 to 255.
- IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.
- IMG_FILTER_EMBOSS: Embosses the image.
- IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method.
- IMG_FILTER_SELECTIVE_BLUR: Blurs the image.
- IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a "sketchy" effect.
- IMG_FILTER_SMOOTH: Makes the image smoother. Use arg1 to set the level of smoothness.
I won't show an example of each type, but here are three short representative applications that use the
imagefilter() function to apply some of the filters: the Emboss Filter (see
Figure 17), the Negate Filter (see
Figure 18), which duplicates the Negate Filter results you saw earlier, and the Smooth Filter (see
Figure 19), which smooths the edges in an image:
Emboss Filter
 | |
Figure 17. Emboss Filter: By enhancing edges and shadows, this filter produces an embossed 3D look. |
// Listing embossPredefFilter.php
<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("tree.jpg");
imagefilter($image,IMG_FILTER_EMBOSS);
imagejpeg($image);
imagedestroy($image);
?>
Negate Filter
 | |
Figure 18. Negate Filter: This built-in filter functions identically to the custom Negate Filter. |
//Listing negatePredefFilter.php
<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("tree.jpg");
imagefilter($image,IMG_FILTER_NEGATE);
imagejpeg($image);
imagedestroy($image);
?>
Smooth Filter
 | |
Figure 19. Smooth Filter: This filter smooths edges and corners to soften an image. |
// Listing smoothPredefFilter.php
<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("tree.jpg");
imagefilter($image,IMG_FILTER_SMOOTH,10);
imagejpeg($image);
imagedestroy($image);
?>