Image fun with PHP – part 2
This post is a demo of what the imagefilter() PHP function can do for you.
The Original

imagefilter() called with different filter constants

Filter: IMG_FILTER_BRIGHTNESS
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_BRIGHTNESS, 5);
imagepng($image, 'img_filter_brightness_5.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_BRIGHTNESS
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_BRIGHTNESS, 50);
imagepng($image, 'img_filter_brightness_50.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_BRIGHTNESS
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_BRIGHTNESS, 100);
imagepng($image, 'img_filter_brightness_100.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_GRAYSCALE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagepng($image, 'img_filter_grayscale.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_CONTRAST
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_CONTRAST, 5);
imagepng($image, 'img_filter_contrast_5.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_CONTRAST
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_CONTRAST, -40);
imagepng($image, 'img_filter_contrast_-40.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_CONTRAST
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_CONTRAST, 50);
imagepng($image, 'img_filter_contrast_50.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_COLORIZE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_COLORIZE, 100, 0, 0);
imagepng($image, 'img_filter_colorize_100_0_0.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_COLORIZE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_COLORIZE, 0, 100, 0);
imagepng($image, 'img_filter_colorize_0_100_0.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_COLORIZE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_COLORIZE, 0, 0, 100);
imagepng($image, 'img_filter_colorize_0_0_100.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_COLORIZE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_COLORIZE, 100, 100, -100);
imagepng($image, 'img_filter_colorize_100_100_-100.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_COLORIZE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_COLORIZE, 50, -50, 50);
imagepng($image, 'img_filter_colorize_50_-50_50.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_EDGEDETECT
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_EDGEDETECT);
imagepng($image, 'img_filter_edgedetect.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_EMBOSS
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_EMBOSS);
imagepng($image, 'img_filter_emboss.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_GAUSSIAN_BLUR
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
imagepng($image, 'img_filter_gaussian_blur.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_SELECTIVE_BLUR
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_SELECTIVE_BLUR);
imagepng($image, 'img_filter_selective_blur.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_MEAN_REMOVAL
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_MEAN_REMOVAL);
imagepng($image, 'img_filter_mean_removal.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_SMOOTH
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_SMOOTH, 5);
imagepng($image, 'img_filter_smooth_5.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_SMOOTH
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_SMOOTH, 50);
imagepng($image, 'img_filter_smooth_50.png');
imagedestroy($image);
?>

Filter: IMG_FILTER_NEGATE
Code to reproduce:
<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_NEGATE);
imagepng($image, 'img_filter_negate.png');
imagedestroy($image);
?>
A lazy way to do sepia
In order to do sepia, first you do grayscale, then colorize. Here are some experiments:

<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 100, 50, 0);
imagepng($image, 'sepia_100_50_0.png');
imagedestroy($image);
?>

<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 100, 70, 50);
imagepng($image, 'sepia_100_70_50.png');
imagedestroy($image);
?>

<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 90, 60, 30);
imagepng($image, 'sepia_90_60_30.png');
imagedestroy($image);
?>

<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 60, 60, 0);
imagepng($image, 'sepia_60_60_0.png');
imagedestroy($image);
?>

<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 90, 90, 0);
imagepng($image, 'sepia_90_90_0.png');
imagedestroy($image);
?>

<?php
$image = imagecreatefrompng('nathalie.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 45, 45, 0);
imagepng($image, 'sepia_45_45_0.png');
imagedestroy($image);
?>
You can read about the right way to do sepia in Wikipedia, but I'd say that the fakes above look pretty good too.
You can try with different values for R, G and B, but hear this - the thing about sepia is that it's brownish-yellow.
Brown is something that has more red, little blue and the green exactly in between the red and the blue. So brown examples would be (200, 100, 0) or (150, 100, 50).
Yellow on the other hand is equal red and green and no blue, like (255, 255, 0). So if you want more brownish sepia, use the first pattern when calling the colorize filter.
About part one
Part one of the image fun is here, it contains code that more or less does the same things, but pixel by pixel, which is very slow, but also works in PHP4.
At the time of writing part one, imagefilter() funtion was probaly only in cvs, not part of the official PHP. imagefilter() is PHP5-only.
This entry was posted on Tuesday, November 13th, 2007 and is filed under images, php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

November 13th, 2007 at 8:27 am
Lol! Looks like you’ve been having endless fun with the GD library. So many gorgeous filters, it’s just a shame that it’s so impractical for PHP really because of the CPU cycles – otherwise I’d certainly implement them in the next imaging system I did. Then again, I may do just that and then sell the website based on its “gorgeous” factor.
November 13th, 2007 at 10:14 am
Stoyan Stefanov’s Blog: Image fun with PHP – part 2…
SToyan Stefanov has posted some examples of what sort of output ……
November 13th, 2007 at 2:55 pm
[...] SToyan Stefanov has posted some examples of what sort of output can be made with the imagefilter function in PHP. [...]
November 13th, 2007 at 3:40 pm
[...] You can read the full story here [...]
November 14th, 2007 at 7:36 am
Nice tutorial
As a good complement, one can use imageconvolution. This function applies a convolution matrix to an image. It helps to apply more custom filters. Future versions will increase the matrix size.
On the news side, the colorize filter supports the alpha channel now. An easy way to add transparency to an image.
Keep the good work
November 14th, 2007 at 11:25 am
[...] phpied.com » Blog Archive » Image fun with PHP – part 2 A demo of what the imagefilter() PHP function can do for you (tags: PHP image gd) [...]
November 14th, 2007 at 3:20 pm
Thanks Pierre, I’ll look into posting about imageconvolution() and help spreading the good news
November 16th, 2007 at 10:53 pm
Wooh! its nice one
November 18th, 2007 at 11:17 am
[...] Image fun part 2 (0 visite) [...]
November 22nd, 2007 at 4:06 am
Good article maybe i would use your ideas on mine image upload script v2
November 26th, 2007 at 9:59 pm
Me again! And yes, you should! Although be careful because your host may be none too happy with all the CPU cycles. Image manipulation is very server intensive stuff.
January 18th, 2008 at 7:08 am
[...] Image fun with PHP – part 2 [...]
January 20th, 2008 at 6:24 pm
That’s a great demo, thanks very much.
I’m just starting a simple tool to add filters on images through PHP, and you’ve saved me to do experiments myself, I’ll go straight from your results.
Thanks again!
January 27th, 2008 at 2:39 am
[...] The phpied blog has some cool demos of the imagefilter() function in particular. Bookmark It Hide Sites [...]
January 28th, 2008 at 7:44 am
This is no doubt great tutorial. Good Work!
March 14th, 2008 at 1:21 pm
Yes, this looks great, but you can do much much more if you will use imagemagick.
April 8th, 2009 at 2:24 am
Is there a way to make theses filters to work with a png image with a transparent background?
Thanks a lot!!!
December 17th, 2009 at 8:04 am
Great Article, I looking for such article.
Thanks
May 5th, 2011 at 3:58 am
[...] http://www.phpied.com/image-fun-with-php-part-2/ [...]
June 13th, 2011 at 1:39 am
[...] photo filters?)How does Instagram develop their filters? (second answer has a javascript library)http://www.phpied.com/image-fun-…http://www.pythonware.com/librar…This answer .Please specify the necessary improvements. Edit [...]
October 1st, 2011 at 3:07 am
image editing…
[...]Image fun with PHP – part 2 / Stoyan’s phpied.com[...]…
October 28th, 2011 at 4:15 pm
PHP Scripts…
[...]Image fun with PHP – part 2 / Stoyan’s phpied.com[...]…
January 1st, 2013 at 7:03 pm
Very nice, thank you !
April 20th, 2013 at 8:56 pm
Wow, great weblog shape! Just how long have you been blogging and site-building intended for? you are making blogs start looking simple. The full look within your web site is amazing, not to mention the content!