Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP GD IMAGE » Isolate a color

Isolate a color

06/06/2011 by Dux   -   not rated

Here is a class to isolate a primary color rendering of a photo black and white.
The choice of using Object-oriented programming instead of a function gives the advantage to add filters or other image effects thereafter.

Instantiation
$obj = new image_effect();
// Optional : ratio size of the working image. default : 2
$obj->multiple = 2;
// Create the working image
$obj->createimg('flowers.jpg');
// Apply the filter (red, green, blue)
$obj->color_isolate('blue');
// Send image to browser or into a file if path is specified
$obj->sendimg( path to the file optional );

original image of the demonstration

See a demonstration of this script in action :

Open in a new window     Open in the same window

source code



 
<?php
ini_set
('memory_limit', '128M');


class
image_effect {
   
   
// Size of the tempory image (according to the power of your server)
   
public $multiple = 2;
   
   public function
createimg($url_img) {
   
       
// Detect format and create $img
       
$part = pathinfo($url_img);
       
$ext = strtolower($part['extension']);
       if (
$ext == "jpg" or $ext == "jpeg") $this->img = imagecreatefromjpeg($url_img);
       if (
$ext == "gif") $this->img = imagecreatefromgif($url_img);
       if (
$ext == "png") $this->img = imagecreatefrompng($url_img);
       
       
       
// Create a bigger image for working with 256 colors
       
$this->img2 = $this->img;
       list(
$this->x,$this->y) = getimagesize($url_img);
       
$this->img = imagecreatetruecolor($this->x * $this->multiple, $this->y * $this->multiple);
       
imagecopyresampled($this->img, $this->img2, 0, 0, 0, 0, $this->x * $this->multiple, $this->y * $this->multiple, $this->x, $this->y);
   }
   
   public function
color_isolate($isolate) {
   
       
// calculating and modification in palette color
       
imagetruecolortopalette($this->img, true, 256);
       for(
$i=0; $i<imagecolorstotal( $this->img ); $i++ ) {
           
$c = ImageColorsForIndex( $this->img, $i );
           if (
$c[$isolate] < $c['green'] or $c[$isolate] < $c['blue'] or $c[$isolate] < $c['red']) $c['red'] = $c['green'] = $c['blue'] = ($c['red'] +$c['green'] + $c['blue']) / 3;
           
imagecolorset( $this->img, $i, $c['red'], $c['green'], $c['blue'] );
       }
       
       
// Back to original size with resample
       
imagecopyresampled($this->img2, $this->img, 0, 0, 0, 0, $this->x, $this->y, $this->x * $this->multiple, $this->y * $this->multiple);
       
$this->img = $this->img2;
   }
   
   
   public function
sendimg($path_file = NULL) {
       
// Send
       
header('Content-Type: image/jpeg');
       
imagejpeg($this->img, $path_file, 80 );
       
imagedestroy($this->img);
   }

}


$obj = new image_effect();
// Optional ratio size of the working image
$obj->multiple = 2;
// Create image from file
$obj->createimg('flowers.jpg');
// applies the effect
$obj->color_isolate('blue');
// Send image to browser or to a file if path is defined
$obj->sendimg(path optional);

?>

Rate this code     |     Copied : 6x

Comments for Isolate a color

No comment
Add a comment Nickname
ZOOM


PHP watermark
Image class resize and rounded mask
Very simple drag and drop upload form
Rotate and scale with CSS
Isolate a color
Adjust the rate of image colors
Simple image gallery PHP
Automatically adjust brightness contrast of an image