Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT] Image Filters
#1
In this tutorial I'll show you how to manipulate images using the function imagefilter().

bool imagefilter ( resource $image , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 [, int $arg4 ]]]] )

I'll use my current signature to show you what we can do.
The function uses 4 argument variables, all of the type INT. It depends on which $filtertype you choose, how many arguments you need to define..

First we need an image resource, since the image I'm using is a PNG one, I use imagecreatefrompng() to create the needed image resource.

PHP Code:
$img imagecreatefrompng("sig.png");

/*
 - We will place our imagefilter() function between those lines, 
 - before we output it with the imagepng() function
*/

header("Content-type: image/png");
imagepng($img);
imagedestroy($img); 

And this is the original image;
[Image: AK4VL.png]

To start off simple, I'll show you how to use imagefilter() to reverse image colors.
For this case we use the $filtertype IMG_FILTER_NEGATE, this type doesn't require any other arguments set..
The function would be called like this;

PHP Code:
imagefilter($imgIMG_FILTER_NEGATE); 

Our image would now look like this;
[Image: mzmxK.png]

So far, it's simple isn't it...
Now let me show you how to use imagefilter() to convert the image to Grayscale...
This one is also pretty simple, since this filter type also doesn't require any other arguments defined.

PHP Code:
imagefilter($imgIMG_FILTER_GRAYSCALE); 

The effect of the function would turn the image into something like this;
[Image: uy6Cy.png]

Our next filter type is IMG_FILTER_BRIGHTNESS, which requires $arg1 defined.
$arg1 is used to set the level of brightness, the integer value can be set from -255 to 255. (dark/light)
I will set the brightness level to 100.

PHP Code:
imagefilter($imgIMG_FILTER_BRIGHTNESS100); 

The image now looks like this;
[Image: 8JZF3.png]

We also have a Contrast filter, IMG_FILTER_CONTRAST, uses $arg1 to set the lavel for the contrast.
I will set the level to -25.

PHP Code:
imagefilter($imgIMG_FILTER_CONTRAST, -25); 

After executing this filter, the image would look like this;
[Image: sb1Uq.png]

Next on our filter type list is, IMG_FILTER_COLORIZE, this filter uses all 4 argument variables.
$arg1 is used to set level for the RED color, $arg2 is used for the GREEN color, $arg3 is used to set the lavel of the BLUE color and the last
argument is used to set the alpha level of the defined RGB value. ($arg4 can be set from 0 to 127, 0 is completely opaque and 127 completely transparent)

I will add a violet color and set it's alpha level to 60.

PHP Code:
imagefilter($imgIMG_FILTER_COLORIZE1007518060); 

The image would be now;
[Image: 0HcDi.png]


Our next filter type, IMG_FILTER_EDGEDETECT, doesn't use any other arguments.

PHP Code:
imagefilter($imgIMG_FILTER_EDGEDETECT); 

The converted image looks like this;
[Image: QcDlE.png]


The next filter is, IMG_FILTER_EMBOSS, which also doesn't need any other arguments.

PHP Code:
imagefilter($imgIMG_FILTER_EMBOSS); 

The image with this filter applied would look like this;
[Image: VrD4U.png]

We can also blur the image, and there are 2 filter types for this case, I will only talk about gaussian blur.
This filter type doesn't use any arguments....

PHP Code:
imagefilter($imgIMG_FILTER_GAUSSIAN_BLUR); 

The blured image looks like this;
[Image: 5ZmJT.png]

Going to the naxt filter, IMG_FILTER_MEAN_REMOVAL, this will use mean removal to achieve a "sketchy" effect.
The filter type doesn't use any arguments either.

PHP Code:
imagefilter($imgIMG_FILTER_MEAN_REMOVAL); 


Our "sketchy" image would look like this;
[Image: lxGAQ.png]


Next filter is, IMG_FILTER_SMOOTH, this filter type uses $arg1 to set the smoothness level. 0 = Blured => 100 = Smooth

PHP Code:
imagefilter($imgIMG_FILTER_SMOOTH50); 

Image now looks like this; (not a real difference ;) )
[Image: xcZD6.png]

And we are now at the last filter type, IMG_FILTER_PIXELATE, this filter uses 2 arguments, $arg1 is required while $arg2 by default is defined as FALSE (0).
$agr1 is used to set the block size in pixels and $arg2 is used to enable/disable advanced pixelation effect.

I'll set the block size to 5 pixel ($arg1) and use the advanced pixelation effects.

PHP Code:
imagefilter($imgIMG_FILTER_PIXELATE5TRUE); 

The image now looks like this;
[Image: wJHro.png]


END
Thank you for reading and have fun Photoshop-ing with PHP. Big Grin

ps:
I want to thank Tasemu again for the Signature... Thank you! ;)
And please notify me if images are not loading, thank you!
Reply
#2
Great tutorial! The pixelate one is cool Smile
Reply
#3
(01-11-2010, 06:17 PM)Dutchcoffee Wrote: Great tutorial! The pixelate one is cool Smile

Awsome, Thanks...
I too think PIXELATE is the coolest one to use, you can use it for funny things ;)
I also like the EDGEDETECT...
Reply
#4
Oh ! I didn't that at all !
Do you know how much CPU usage does it cost ?
I mean, i guess this kind of operation aren't optimized that much like Photoshop, so, i'm afraid for the CPU ...
I think i'm gonna test with my local Apache server Tongue
Reply
#5
Ah, dude, that is awesome! I love the Pixelated one aswell.
At the top will be the same place you hang from.
Reply
#6
(01-16-2010, 07:01 PM)Spl3en Wrote: Oh ! I didn't that at all !
Do you know how much CPU usage does it cost ?
I mean, i guess this kind of operation aren't optimized that much like Photoshop, so, i'm afraid for the CPU ...
I think i'm gonna test with my local Apache server Tongue

I'm not sure how much, I always keep the use of image functions low as possible.. Therefor if you free up the memory with imagedestroy() you'll be fine if you're the only one having access to the script...

Don't forget to destroy every image resource you create when you don't use them anymore, imagedestroy()...

(01-16-2010, 07:05 PM)trilobyte- Wrote: Ah, dude, that is awesome! I love the Pixelated one aswell.

Thanks man, glad you like it! Big Grin
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)