PHP GD image negative Color


 

PHP GD image negative Color

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

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

Ads