(PHP 5)
imagefilter — Applies a filter to an image
imagefilter() applies the given filter filtertype on the image .
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
filtertype can be one of the following:
Returns TRUE on success or FALSE on failure.
Version | Description |
---|---|
5.2.5 | Alpha support for IMG_FILTER_COLORIZE was added. |
Example #1 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 #2 imagefilter() brightness example
<?php
$im = imagecreatefrompng('sean.png');
if($im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 20))
{
echo 'Image brightness changed.';
imagepng($im, 'sean.png');
imagedestroy($im);
}
else
{
echo 'Image brightness change failed.';
}
?>
Example #3 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');
imagedestroy($im);
}
else
{
echo 'Green shading failed.';
}
?>
Example #4 imagefilter() negate example
<?php
// Define our negate function so its portable for
// php versions without imagefilter()
function negate($im)
{
if(function_exists('imagefilter'))
{
return imagefilter($im, IMG_FILTER_NEGATE);
}
for($x = 0; $x < imagesx($im); ++$x)
{
for($y = 0; $y < imagesy($im); ++$y)
{
$index = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($index);
$color = imagecolorallocate($im, 255 - $rgb['red'], 255 - $rgb['green'], 255 - $rgb['blue']);
imagesetpixel($im, $x, $y, $color);
}
}
return(true);
}
$im = imagecreatefromjpeg('kalle.jpg');
if($im && negate($im))
{
echo 'Image successfully converted to negative colors.';
imagejpeg($im, 'kalle.jpg', 100);
imagedestroy($im);
}
else
{
echo 'Converting to negative colors failed.';
}
?>
Note: This function is only available if PHP is compiled with the bundled version of the GD library.