Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

How To Create and Apply Image Filters in PHP : Page 3

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.


advertisement
Color-Altering Filters
All the filters in this group work by altering colors. Each filter applies a specific operation to all or a subset of the image pixels, resulting in an entirely new and often quite different image from the original.

Grayscale Filter
By removing all the color values and replacing them with shades of gray, you can create nice-looking black and white images (see Figure 5).

 
Figure 5. Grayscale Filter: The grayscale filter removes color from images.

// Listing grayscale_filter.php <?php $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++){ // get the pixel color at i,j $rgb = imagecolorat($im, $i, $j); // get the individual color values $r = $rgb&0x00FF0000; $r = $r >> 16; $g = $rgb&0x0000FF00; $g = $g >> 8; $b = $rgb&0x0000FF; // calculate the grayscale $bw=($r+$g+$b)/3; $result = (0x000000FF<<24)|($bw<<16)|($bw<<8)|$bw; // create the new color values $new_r = ($result >> 16) & 0xFF; $new_g = ($result >> 8) & 0xFF; $new_b = $result & 0xFF; // assign the grayscale color $new_color = imagecolorallocate($im, $new_r, $new_g, $new_b); imagesetpixel($im, $i, $j, $new_color); } } header("Content-type: image/jpeg"); imagejpeg($im); ?>

Negate Filter
 
Figure 6. Negate Filter: This filter reverses, or negates the colors in an image.
This application creates a "negate" filter (see Figure 6) by applying an exclusive OR operation to the red, green, and blue portions of the color while leaving the alpha channel intact:



// Listing negate_filter.php <?php $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); $result = ($rgb & 0xff000000)+($rgb & 0xffffff)^0xffffff; $new_r = ($result >> 16) & 0xFF; $new_g = ($result >> 8) & 0xFF; $new_b = $result & 0xFF; $new_color = imagecolorallocate($im, $new_r, $new_g, $new_b); imagesetpixel($im, $i, $j, $new_color); } } header("Content-type: image/jpeg"); imagejpeg($im); ?>

Flea Filter
 
Figure 7. Flea Filter: An interesting colorization effect, this filter works by OR'ing the existing colors in an image with a random color.
A flea filter is a random color value OR'd with the existing color values in an image. It produces a colorful and interesting effect (see Figure 7):

// Listing flea_filter.php <?php $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); $new_r = rand(0,255); $new_g = rand(0,255); $new_b = rand(0,255); $new_color = imagecolorallocate($im, $new_r, $new_g, $new_b); $new_rgb = $rgb|$new_color; imagesetpixel($im, $i, $j, $new_rgb); } } header("Content-type: image/jpeg"); imagejpeg($im); ?>

Fog Filter
 
Figure 8. Fog Filter: By using the alpha channel value (which requires the PNG format instead of JPG), this filter applies a fog-like film to an image.
As the name implies, a fog filter applies a hazy film over an image (see Figure 8). Note that in this case you have to covert the tree.jpg file to a Portable Network Graphics (PNG) file (tree.png) before beginning the operation, because images in PNG format can use alpha channel values while images in JPEG format do not.

// Listing fog_filter.php <?php $im = imagecreatefrompng("tree.png"); $size = getimagesize("tree.png"); $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; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; //Turn off transparency blending imagealphablending($im, false); //Create a new transparent color for image $color = imagecolorallocatealpha($im, $r, $g, $b, 100); //Set the new color imagesetpixel($im, $i, $j, $color); //Restore transparency blending imagesavealpha($im, true); } } header('content-type: image/png'); imagepng($im); ?>



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap