PHP - Image filters
One of many advantages of PHP5 over PHP4 is ability to process images with various image filters.
According to PHP manual, you can
1. Convert image to negative
2. Convert image to grayscale
3. Set the brightness of an image
4. Set the contrast of an image
5. Colorize image. This filter will change color saturations in your image.
6. Edge detection. This filter will highlight the edges of objects on image
7. Emboss. Filter will create a relief of image.
8. Blur. Blurs the image. This filter have two variations, Gausian blur and Selective blur.
9. Mean removal. This filter will make your image looks like a sketch.
10. Smooth. Filter will make object edges transitions smoother.
It is very possible that this list will become larger with new versions of GD library and new versions of a PHP. It is also possible that there are other filters not documented in PHP manual. Watch PHP manual for list of new filters, and detailed description of filters parameters.
In this example, we will create a script which can apply filters to an image. You will see JavaScript code in the example, but I'm using it just to create request string easier. When you start this example, you will be able to see request string bellow the images.
You can send up to four parameters to the script. These are "filter", "arg1", "arg2" and "arg3". Just first one, "filter" is required. Depending on type of filter you are using, you will send other parameters.
$im = imagecreatefromjpeg('image.jpg');
if ($_REQUEST['filter'] == 'negative')
imagefilter($im, IMG_FILTER_NEGATE);
else
if ($_REQUEST['filter'] == 'grayscale')
imagefilter($im, IMG_FILTER_GRAYSCALE);
else
if ($_REQUEST['filter'] == 'brightness')
imagefilter($im, IMG_FILTER_BRIGHTNESS, $_REQUEST['arg1']);
else
if ($_REQUEST['filter'] == 'contrast')
imagefilter($im, IMG_FILTER_CONTRAST, $_REQUEST['arg1']);
else
if ($_REQUEST['filter'] == 'colorize')
imagefilter($im, IMG_FILTER_COLORIZE, $_REQUEST['arg1'], $_REQUEST['arg2'], $_REQUEST['arg3']);
else
if ($_REQUEST['filter'] == 'edgedetect')
imagefilter($im, IMG_FILTER_EDGEDETECT);
else
if ($_REQUEST['filter'] == 'emboss')
imagefilter($im, IMG_FILTER_EMBOSS);
else
if ($_REQUEST['filter'] == 'gaussian_blur')
imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
else
if ($_REQUEST['filter'] == 'selective_blur')
imagefilter($im, IMG_FILTER_SELECTIVE_BLUR);
else
if ($_REQUEST['filter'] == 'mean_removal')
imagefilter($im, IMG_FILTER_MEAN_REMOVAL);
else
if ($_REQUEST['filter'] == 'smooth')
imagefilter($im, IMG_FILTER_SMOOTH, $_REQUEST['arg1']);
imagejpeg($im);
imagedestroy($im);
?>
Related Marakana Courses
- PHP and MySQL Bootcamp Training