Share on Google+Share on Google+

PHP GD image negative Color

Advertisement
This example shows how to set the negative color of an image in php gd.

<?php

function negate($image)

{

if(function_exists('imagefilter'))

{

return imagefilter($image, IMG_FILTER_NEGATE);

}

for($x = 0; $x < imagesx($image); ++$x)

{

for($y = 0; $y < imagesy($image); ++$y)

{

$index = imagecolorat($image, $x, $y);

$rgb = imagecolorsforindex($index);

$color = imagecolorallocate($image, 255 - $rgb['red'], 255 - $rgb['green'], 255 - $rgb['blue']);

imagesetpixel($im, $x, $y, $color);

}

}

return(true);

}

$image = imagecreatefromjpeg('image1.jpg');

if($image && negate($image))

{

echo 'Image successfully converted to negative colors.';

imagejpeg($image, 'image1.jpg', 100);

imagedestroy($image);

}

else

{

echo 'Negative color conversion failed.';

}

?>

After running the program you will get the following output

Advertisements

Advertisement

Posted on: November 12, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.