function invert_image ($image
{
$image_width = imagesx$image);
$image_height = imagesy$image);
// if the image is not true color, make it so
if (!imageistruecolor$image)) {
$image2 = imagecreatetruecolor$image_width, $image_height);
imagecopy$image2$image,0,0,0,0,$image_width$image_height);
$image = $image2
}
// loop through all the pixels
for ($h = 0; $h < $image_height; $h++) {
for ($w = 0; $w < $image_width; $w++) {
// get the color at this pixel
$color = imagecolorsforindex$image, imagecolorat$image, $w, $h));
// invert the color
$color'red'] = 255 - $color'red'];
$color'green'] = 255 - $color'green'];
$color'blue'] = 255 - $color'blue'];
// create the new color
$new_color = imagecolorallocate$image, $color'red'], $color'green'], $color'blue']);
// set the color
imagesetpixel$image, $w, $h, $new_color);
}
}
return $image
}