By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,708 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,708 IT Pros & Developers. It's quick & easy.

Need help with a GD function

P: n/a
I found this function on It is supposed to colorize a JPEG
image by covering it with a transparent colored overlay. I'm trying to
implement it, but whatever color value I pass it always it always colorizes
the image in greyscale.

I'm probably passing the color values incorrectly, but I'm not sure what
kind of data the function is expecting. The page where I found the function
doesn't provide any solutions.

Any help would be extremely appreciated.
Here's the function :
// function creates a layover in the specified color
// use it to output images in accordance with the basecolor
// of each issue

function imagecolorize(&$im,&$col,$pct) {
// Get the image's width
$im_w = imagesx($im);
// Get the image's height
$im_h = imagesy($im);
// Set a pixel with the color, so we can get it easily
$setpixel = imagesetpixel($im,$im_w,0,$col);
// Get the color
$index = imagecolorat($im,$im_w,0);
// Find the color in the index
$rgb = imagecolorsforindex($im,$index);
// Get the red value
$r = $rgb["red"];
// Get the green value
$g = $rgb["green"];
// Get the blue value
$b = $rgb["blue"];
// Create the layover
$layover = imagecreate($im_w,$im_h);
// Allocate the color on this image
$color = imagecolorallocate($layover,$r,$g,$b);
// Fill the image with the new color (this really isn't needed)
$fill = imagefill($layover,0,0,$color);
// Merge the layover on top of the older image
$merge = imagecopymerge($im,$layover,0,0,0,0,$im_w,$im_h,$p ct);
imagedestroy($layover); // Destroy the layover
return $merge;
Here's my implementation :
// split color value into rgb parts
$rgb = chunk_split('ff0000', 2);

// convert hex values to decimal
$rgb[0] = hexdec($rgb[0]);
$rgb[1] = hexdec($rgb[1]);
$rgb[2] = hexdec($rgb[2]);

$img = imagecreatefromjpeg('logoFM7201081767006.jpg');
$im = imagecolorize($img, $rgb, 20);

Thank you,
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.