|
SubMenu
Tutorial Stats
Made by: ForterOn: 16-5-2007 15:20Total views: 2434Replys: 0Catagory: EntertainingType: PHPSubject: imagesAdvertisement
|
PHP Tutorial
Image color transformationThis tutorial will show you some basic image color transformation methods, one to make a grayscale image and one to invert the colors. The code is done in PHP with the GD library, so it's easy to try it yourself. Please realize that this is not the fastest way to make these transformations and that it's not a very good idea to use PHP as an direct output to your site's visitors. The normal image
I've got a nice photograph of a flower, so lets use that as our normal image. Its
200x150 pixels, so php shouldn't encounter any problems. If you want to use larger
images then make sure you alter the memory limit, something which should be easy to fix
with htaccess.
<?php
header("Content-Type: image/jpeg");
$image = imagecreatefromjpeg("flower_normal.jpg");
imagejpeg($image);
?>
This piece of code sets the header to image/jpg, so the browser knows that an image is coming. Then the image flower_normal.jpg is loaded into $image, and $image is send to the browser. This should be pretty easy to understand. Invert colorsRaster 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. The description is a bit f*cked up, but you'll see what i mean in the code:
<?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);
?>
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.
GrayscaleThis is off course a very simple way to mess around with image coloring, so i'll show another example in which we will be changing the image to grayscale. The idea is that if the values of red, green and blue are the same, the color will be grayish. Something like (255, 255, 255) resembles white and (0, 0, 0) black. If we want set our image to grayscale then we only have to add the values of red green and blue together and divide this by three. The code will then look like this:
<?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));
$grayed_color = ($colors['red'] + $colors['green'] + $colors['blue']) / 3;
$new_color = imagecolorallocate($image, $grayed_color, $grayed_color, $grayed_color);
imagesetpixel($image, $w, $h, $new_color);
}
}
imagejpeg($image, NULL, 80);
?>
This example takes the color for each pixel again, then calculates the average
value. This value is then
transformed into the color format using the
imagecolorallocate() function. The output will be something similar to the image on the
right side of this text.
|