I did up this little bit of code. It reads in an image and converts it to grayscale but preserves everything within the specified tolerance of the specified color. Tolerance works by creating a color orb with $tolerance radius so sometimes it preserves unexpected colors. If anyone knows how to fix this let me know.

<?php
$image_dir = 'images/';
$_GET['filename'] = 'stellar2.jpg';

if(!array_key_exists('filename',$_GET)) {die('no file specified');}
elseif(!is_dir($image_dir)) {die('directory not found');}
elseif(!file_exists($image_dir . $_GET['filename'])) {die('file not found');}
elseif(!is_readable($image_dir . $_GET['filename'])) {die('file not readable');}

$filename = $image_dir . $_GET['filename'];
$preserve_color = array(68,80,44); //red, green, blue
$tolerance = 25; //if no color code is off by more then this it will be preserved

list($width,$height,$type,$text) = getimagesize($filename);

//we're only going to do jpeg & png types
switch($type) {
    case 2:
        $orig_im = imagecreatefromjpeg($filename);
        break;
    case 3:
        $orig_im = imagecreatefrompng($filename);
        break;
    default:
        die();
} //end switch

//go down the picture
for($y=0;$y<$height;$y++) {
    //go across the picture
    for($x=0;$x<$width;$x++) {
        //get the rgb values
        $rgb   = imagecolorat($orig_im,$x,$y);
        $red   = ($rgb >> 16) & 0xFF;
        $green = ($rgb >> 8) & 0xFF;
        $blue  = $rgb & 0xFF;

    //if we have to convert this pixel
    if(abs($red - $preserve_color[0]) > $tolerance &&
       abs($green - $preserve_color[1]) > $tolerance &&
       abs($blue - $preserve_color[2]) > $tolerance) {  
        //create the gray value
        $gray = round(.299*$red + .598*$green + .114*$blue);    
        $grayR = $gray << 16;
        $grayG = $gray << 8;
        $grayB = $gray;
        $grayColor = $grayR | $grayG | $grayB;

        //reset the pixel color
        imagesetpixel($orig_im,$x,$y,$grayColor);
        imagecolorallocate($orig_im,$gray,$gray,$gray);
    } //end if
} //end for
} //end for 


$new_im = imagecreate($width,$height);

//copy the new image included here for convience in making this 
//save the file to the disk
imagecopy($new_im,$orig_im,0,0,0,0,$width,$height);

//send the new image to the browser
header('Content-type: image/png');
imagepng($new_im);

imagedestroy($new_im);
imagedestroy($orig_im);
?>
    19 days later

    this got me somewhere:

        <?php    
    //if we have to convert this pixel /*
    if(abs($red - $preserve_color[0]) > $tolerance && abs($green - $preserve_color[1]) > $tolerance && abs($blue - $preserve_color[2]) > $tolerance) */ if (!($red<$preserve_color[0]+$tolerance && $red>$preserve_color[0]-$tolerance && $green<$preserve_color[1]+$tolerance && $green>$preserve_color[1]-$tolerance && $blue<$preserve_color[2]+$tolerance && $blue>$preserve_color[2]-$tolerance)) { $gray=($red+$green+$blue)/3; // $gray = round(.299*$red + .598*$green + .114*$blue);?>

    but there is still some strangeness occuring, i dont know why, be extra careful when choosing $preserve_color..

      That code is nice but what I'm really wondering is if its possiable to work with colorizing without the GD Librarys..probably isn't because I'm working on this Font Sprite script so people won't have to download the font etc it posts 40 byte gifs instead. And I don't want to make a bunch of sprites that have different color that'll slow things down even more and be a waste of good hard drive capacity. So what I've been thinking if I can change the color of the images instead of making new images but I don't want to install the GD Librarys 🙁 Anyways back to the topic...a very nice script 😃

        Write a Reply...