Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Image Functions : imagefilter

imagefilter

Applies a filter to an image (PHP 5)
bool imagefilter ( resource image, int filtertype [, int arg1 [, int arg2 [, int arg3 [, int arg4]]]] )

Example 1011. imagefilter() grayscale example

<?php
$im
= imagecreatefrompng('dave.png');
if (
$im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
   echo
'Image converted to grayscale.';
   
imagepng($im, 'dave.png');
} else {
   echo
'Conversion to grayscale failed.';
}

imagedestroy($im);
?>

Example 1012. imagefilter() brightness example

<?php
$im
= imagecreatefrompng('sean.png');
if (
$im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 20)) {
   echo
'Image brightness changed.';
   
imagepng($im, 'sean.png');
} else {
   echo
'Image brightness change failed.';
}

imagedestroy($im);
?>

Example 1013. imagefilter() colorize example

<?php
$im
= imagecreatefrompng('philip.png');

/* R, G, B, so 0, 255, 0 is green */
if ($im && imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)) {
   echo
'Image successfully shaded green.';
   
imagepng($im, 'philip.png');
} else {
   echo
'Green shading failed.';
}

imagedestroy($im);
?>

Code Examples / Notes » imagefilter

webmaster

This will only work if you have php5. For php4, you'll have to use the sepia function set webmaster at qudi dot de suggested.

nancy

This routine was just what I was looking for, I wanted web admin users to be able to recolour their uploaded photos (to go with a news item) either a blue tint or sepia to match the appearance of other colours used on the website.
Using a form with a select box containing the RGB values, I can give them the option of either of the two tints or no colourization at all, plus resize their images to the viewing size and a thumbnail image on the fly without having to use any other image editing software.


admin

this is a sepia function using microsoft's definition
<?php
function imagesepia( $img ) {
$total = imagecolorstotal( $img );
for ( $i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
$red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 ) / 1.351;
$green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ) / 1.203;
$blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ) / 2.140;
imagecolorset( $img, $i, $red, $green, $blue );
}
}
?>


panuworld

The documentation misses the exact meaning and valid ranges of the arguments for ImageFilter(). According to the 5.2.0 sources the arguments are:
IMG_FILTER_BRIGHTNESS
-255 = min brightness, 0 = no change, +255 = max brightness
IMG_FILTER_CONTRAST
-100 = max contrast, 0 = no change, +100 = min contrast (note the direction!)
IMG_FILTER_COLORIZE
Adds (subtracts) specified RGB values to each pixel. The valid range for each color is -255...+255, not 0...255. The correct order is red, green, blue.
-255 = min, 0 = no change, +255 = max
This has not much to do with IMG_FILTER_GRAYSCALE.
IMG_FILTER_SMOOTH
Applies a 9-cell convolution matrix where center pixel has the weight arg1 and others weight of 1.0. The result is normalized by dividing the sum with arg1 + 8.0 (sum of the matrix).
any float is accepted, large value (in practice: 2048 or more) = no change
ImageFilter seem to return false if the argument(s) are out of range for the chosen filter.


trucex email over

It appears that imagefilter doesn't play nice with apha. If you run an imagefilter on a transparent image it'll return a black image...similar to a lot of Photoshop plugins do.

fananf

If you're looking for fast sepia effect that can be used for on-the-fly thumbnails generation you can't use sophisticated functions. The faster and much better way than described by webmaster at qudi dot de in the note from 31-Jan-2006 is applying colorize filter AFTER grayscale.
<?php
(...)
imagefilter($yourimage, IMG_FILTER_GRAYSCALE); imagefilter($yourimage, IMG_FILTER_COLORIZE, 90, 60, 40);
(...)
?>
I used (90,60,40) for my sepia after couple of tests, however, if you need darker or lighter just check what suits you best.


kees

From what i have been able to find from this function, it accepts the following arguments:
       IMG_FILTER_NEGATE
       IMG_FILTER_GRAYSCALE
       IMG_FILTER_EDGEDETECT
       IMG_FILTER_GAUSSIAN_BLUR
       IMG_FILTER_SELECTIVE_BLUR
       IMG_FILTER_EMBOSS
       IMG_FILTER_MEAN_REMOVAL
The following arguments need one or more arguments.
       IMG_FILTER_SMOOTH, -1924.124
       IMG_FILTER_COLORIZE, -127.12, -127.98, 127
       IMG_FILTER_CONTRAST, -90
       IMG_FILTER_BRIGHTNESS, 98
       
I haven't tested them all, the names speak for themselves.


webmaster

for a quick, ok-looking, sepia-effect (also in php4) I just use this little fellow, since a real implementation of sepia was just way too slow.
function pseudosepia(&$im,$percent){
     $sx=imagesx($im);
     $sy=imagesy($im);
     $filter=imagecreatetruecolor($sx,$sy);
     $c=imagecolorallocate($filter,100,50,50);
     imagefilledrectangle($filter,0,0,$sx,$sy,$c);
     imagecopymerge($im,$filter,0,0,0,0,$sx,$sy,$percent);
}


vdepizzol

Examples using imagefilter():
<?php
$im = imagecreatefrompng('dave.png');
if ($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
   echo 'Image converted to grayscale.';
   imagepng($im, 'dave.png');
} else {
   echo 'Conversion to grayscale failed.';
}
imagedestroy($im);
?>
/////////////////////////////
<?php
$im = imagecreatefrompng('sean.png');
if ($im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 20)) {
   echo 'Image brightness changed.';
   imagepng($im, 'sean.png');
} else {
   echo 'Image brightness change failed.';
}
imagedestroy($im);
?>
/////////////////////////////
<?php
$im = imagecreatefrompng('philip.png');
/* R, G, B, so 0, 255, 0 is green */
if ($im && imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)) {
   echo 'Image successfully shaded green.';
   imagepng($im, 'philip.png');
} else {
   echo 'Green shading failed.';
}
imagedestroy($im);
?>


santibari

A colorize algorithm wich preserves color luminosity (i.e black
will output black, and white will output white).
This works in PHP4 and is great for customizing interfaces
dinamically.
<?php
function colorize($img_src,$img_dest, $r, $g, $b)
{
if(!$im = imagecreatefromgif($img_src))
 return "Could not use image $img_src";

//We will create a monochromatic palette based on
//the input color
//which will go from black to white
//Input color luminosity: this is equivalent to the
//position of the input color in the monochromatic
//palette
$lum_inp=round(255*($r+$g+$b)/765); //765=255*3
//We fill the palette entry with the input color at its
//corresponding position
$pal[$lum_inp]['r']=$r;
$pal[$lum_inp]['g']=$g;
$pal[$lum_inp]['b']=$b;
//Now we complete the palette, first we'll do it to
//the black,and then to the white.
//FROM input to black
//===================
//how many colors between black and input
$steps_to_black=$lum_inp;
//The step size for each component
if($steps_to_black)
{
$step_size_red=$r/$steps_to_black;
$step_size_green=$g/$steps_to_black;
$step_size_blue=$b/$steps_to_black;
}
for($i=$steps_to_black;$i>=0;$i--)
{
$pal[$steps_to_black-$i]['r']=$r-round($step_size_red*$i);
$pal[$steps_to_black-$i]['g']=$g-round($step_size_green*$i);
$pal[$steps_to_black-$i]['b']=$b-round($step_size_blue*$i);
}
//From input to white:
//===================
//how many colors between input and white
$steps_to_white=255-$lum_inp;
if($steps_to_white)
 {
 $step_size_red=(255-$r)/$steps_to_white;
 $step_size_green=(255-$g)/$steps_to_white;
 $step_size_blue=(255-$b)/$steps_to_white;
 }
else
 $step_size_red=$step_size_green=$step_size_blue=0;
//The step size for each component
for($i=($lum_inp+1);$i<=255;$i++)
 {
 $pal[$i]['r']=$r + round($step_size_red*($i-$lum_inp));
 $pal[$i]['g']=$g + round($step_size_green*($i-$lum_inp));
 $pal[$i]['b']=$b + round($step_size_blue*($i-$lum_inp));
 }
//--- End of palette creation
//Now,let's change the original palette into the one we
//created
for($c = 0; $c < $palette_size; $c++)
{
$col = imagecolorsforindex($im, $c);          
$lum_src=round(255*($col['red']+$col['green']
               +$col['blue'])/765);
$col_out=$pal[$lum_src];
imagecolorset($im, $c, $col_out['r'],
                               $col_out['g'],
                               $col_out['b']);
}
//save the image file
imagepng($im,$img_dest);
imagedestroy($im);
}//end function colorize
?>


a php user

http://www.hudzilla.org/phpbook/read.php/11_2_15
for more detailed info, and some <i>arg</i> guidelines.


Change Language


Follow Navioo On Twitter
gd_info
getimagesize
image_type_to_extension
image_type_to_mime_type
image2wbmp
imagealphablending
imageantialias
imagearc
imagechar
imagecharup
imagecolorallocate
imagecolorallocatealpha
imagecolorat
imagecolorclosest
imagecolorclosestalpha
imagecolorclosesthwb
imagecolordeallocate
imagecolorexact
imagecolorexactalpha
imagecolormatch
imagecolorresolve
imagecolorresolvealpha
imagecolorset
imagecolorsforindex
imagecolorstotal
imagecolortransparent
imageconvolution
imagecopy
imagecopymerge
imagecopymergegray
imagecopyresampled
imagecopyresized
imagecreate
imagecreatefromgd2
imagecreatefromgd2part
imagecreatefromgd
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromstring
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
imagecreatetruecolor
imagedashedline
imagedestroy
imageellipse
imagefill
imagefilledarc
imagefilledellipse
imagefilledpolygon
imagefilledrectangle
imagefilltoborder
imagefilter
imagefontheight
imagefontwidth
imageftbbox
imagefttext
imagegammacorrect
imagegd2
imagegd
imagegif
imagegrabscreen
imagegrabwindow
imageinterlace
imageistruecolor
imagejpeg
imagelayereffect
imageline
imageloadfont
imagepalettecopy
imagepng
imagepolygon
imagepsbbox
imagepsencodefont
imagepsextendfont
imagepsfreefont
imagepsloadfont
imagepsslantfont
imagepstext
imagerectangle
imagerotate
imagesavealpha
imagesetbrush
imagesetpixel
imagesetstyle
imagesetthickness
imagesettile
imagestring
imagestringup
imagesx
imagesy
imagetruecolortopalette
imagettfbbox
imagettftext
imagetypes
imagewbmp
imagexbm
iptcembed
iptcparse
jpeg2wbmp
png2wbmp
eXTReMe Tracker