Filtering Image Streams with the GD Library in PHP - More graphic filters with the imagefilter() function
(Page 4 of 4 )
As I mentioned in the previous section, the "imagefilter()" function provides PHP developers with a number of other graphic filters for controlling certain characteristics of a selected image stream. Of course, as you might guess, the application of these additional graphic filters can be easily performed by specifying the type of effect that must be implemented when calling the function.
Below I coded some illustrative examples which demonstrate how to apply different graphic filters to the same sample image that you saw in the previous section. You should notice that each code sample is accompanied by the respective resulting image after applying the appropriate filter.
This being said, here are the aforementioned examples, so take some time and have a look at them, please:
// example of 'imagefilter()' function - Colorizes the image
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_COLORIZE,255,0,0)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagefilter()' function - Uses edge detection to
highlight the edges in the image
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_EDGEDETECT)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagefilter()' function - Embosses the image
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_EMBOSS)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagefilter()' function - Blurs the image using
the Gaussian method
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagefilter()' function - Blurs the image
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_SELECTIVE_BLUR)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagefilter()' function - Uses mean removal to
achieve a "sketchy" effect
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_MEAN_REMOVAL)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagefilter()' function - Makes the image smoother
try{
if(!$image=imagecreatefromgif('clouds.gif')){
throw new Exception('Error creating image');
}
// apply filter to image
if(!imagefilter($image,IMG_FILTER_SMOOTH,40)){
throw new Exception('Error applying filter to image');
}
// display image to the browser
header("Content-type: image/gif");
imagegif($image);
// free memory
imagedestroy($image);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

All right, I believe that the hands-on examples coded above should be explanatory enough in themselves to demonstrate the capacity offered by the "imagefilter()" function for applying different graphic filters to a specified image stream.
As with the majority of my articles on PHP development, feel free to modify all the code samples included in this tutorial. This will help you to improve your existing background in using the GD extension within your own PHP applications.
Final thoughts
Finally, this educational journey has come to an end. Hopefully, this series of articles has served as an approachable introduction to using the main functions that come packaged with the GD library. However, if you're looking for a complete reference on this PHP extension, the best place to go is the official PHP site.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |