TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

# How To Create and Apply Image Filters in PHP : Page 2

## Discover how image filters work, how they can spice up your images, how to take advantage of them in PHP, and how to create custom filters that go far beyond the built-in filter effects.

 by Octavia Andreea Anghel
 Feb 23, 2008
 Page 2 of 7
Color Bitwise Operations
The right-shift (>>) and OR (|) bitwise operators play an important role in color representation. For example, as you probably know, a color can be represented by a 32-bit integer; each color component uses a specific number of bits—eight bits in the default color model (RGBA). The following example creates the pure red color A=255,R=255,G=0,B=0 by using the right-shift operator to set the bits for each color component and then using the OR operator to combine the color components to represent the final color.

For example, to form a pure red color, you set the color by shifting the alpha value (255) by 24 positions, the red value (255) by 16 positions, the green value (0) by 8 positions, and the blue value (0) by 0 positions:

 Figure 3. Seeing Red: Here's the sequence of bitwise operations to create a pure red color, showing the RGB color components of an integer.
``````   A=A>>24
R=R>>16
G=G>>8
B=B>>0
``````
Figure 3 shows the operation.

You can also use the bit operators to extract the individual RGB values for a given color. In the following example, the imagecolorat function returns the color of a pixel, and the bit shift operations extract the individual RGB values:

``````   // Listing rgb.php
<?php

\$im = imagecreatefromjpeg("tree.jpg");

\$rgb = imagecolorat(\$im, 1, 10);

\$r = (\$rgb >> 16) & 0xFF;
\$g = (\$rgb >> 8) & 0xFF;
\$b = \$rgb & 0xFF;

echo " r= ".\$r." g= ".\$g." b= ".\$b;
?>
``````
 Figure 4. The Green Band: After removing the red and blue bands of the tree.jpg picture, the image appears in shades of green.
In a larger version of the same technique you can extract the color bands of an entire image. The following code extracts the red, green and blue color bands by looping through each pixel, retrieving the color value for that pixel, and then extracting the various color components. The code to get and assign the red and blue bands is commented out, so the resulting image shows only the green band (see Figure 4):

``````   // Listing RGBbands.php
<?php
//tree.jpg is the initial image
\$im = imagecreatefromjpeg("tree.jpg");
\$size = getimagesize("tree.jpg");
\$L=\$size[0];
\$H=\$size[1];

for(\$j=0;\$j<\$H;\$j++){
for(\$i=0;\$i<\$L;\$i++){
\$rgb = imagecolorat(\$im, \$i, \$j);

//\$r = (\$rgb >> 16) & 0xFF;
//\$red = imagecolorallocate(\$im, \$r, 0, 0);
//imagesetpixel(\$im, \$i, \$j, \$red);

\$g = (\$rgb >> 8) & 0xFF;
\$green = imagecolorallocate(\$im, 0, \$g, 0);
imagesetpixel(\$im, \$i, \$j, \$green);

//\$b = \$rgb & 0xFF;
//\$blue = imagecolorallocate(\$im, 0, 0, \$b);
//imagesetpixel(\$im, \$i, \$j, \$blue);
}
}