Invert colors


Image with Colors Inverted
Raster images are made up of lots of dots called pixels. Every dot has got a color, which is a combination of red green and blue. The red, green and blue can have a value between 0 and 255. If we want to invert the colors of the image then we only have to get the red, green and blue value from each pixel and subtract it from 255.
This piece of code is a bit more complex than the previous example, so i’ll explain
it bit by bit. If you don’t get it than take a look at php.net for a function reference.
The first thing we always do with the GD library and PHP is to create the canvas. For this we use the imagecreatefromjpeg function. As we described it on our first tutorial you can create canvasses either from an image or blank. In this case we load an image from a file.
$image = imagecreatefromjpeg("../images/leno.jpg");
Use the right image path, as PHP will normally look for the file in the folder than your script is. If it is in a different directory rather use the full path, at least during the development stage. You can complicate your script later to find the right directories automatically. PHP will also allow the string to be a URL, so you can pick up your image directly from flickr if you wish! Just check image sizing though if you go this route as your server can slow down with pixel by pixel rendering and calculations.
The first thing we need to determine is the image sizing so we can set our procedure correctly. We do this by using two functions of the GD Library. These functions are the imagesx() and imagesy() .
The important part comes next. We use two for loops to get at every pixel of the image, the current x and y are stored in $w and $h. We then get the color at the current xy position using the imagecolorat() function, we then use the same line of code to place the separate red, green and blue values in an array.
After that we set the colors of the previously obtained array by the simple calculation of 255 minus the original value. And finally we use imagecolorallocate() to create the color from the red, green and blue and use the imagesetpixel() function to set the new color of the pixel. The output is the image on the right side of this text.You can change the image by altering the url in the imagecreatefromjpeg() function, and also the image quality by changing the third argument of the imagejpeg() function.
An interesting thing about images that are inverted is if you stare at them for a while and then replace the image with a black and white image your eyes will perceive the second image as color. See johnsadowski
A practical application by some software is to use color inversion for images that are highlighted. This is not actually a good idea as in many cases the cursor disappears on a black background.
<?php
header("Content-Type: image/jpeg");
$image = imagecreatefromjpeg("flower_normal.jpg");
$image_width = imagesx($image);
$image_height = imagesy($image);
for ($h = 0; $h < $image_height; $h++) {
for ($w = 0; $w < $image_width; $w++) {
$colors = imagecolorsforindex($image, imagecolorat($image, $w, $h));
$colors['red'] = 255 - $colors['red'];
$colors['green'] = 255 - $colors['green'];
$colors['blue'] = 255 - $colors['blue'];
$new_color = imagecolorallocate($image, $colors['red'], $colors['green'], $colors['blue']);
imagesetpixel($image, $w, $h, $new_color);
}
}
imagejpeg($image, NULL, 80);
?>
yannis is
Email this author | All posts by yannis