tags:

views:

626

answers:

4

I'd like to change the "hue" of an image using the PHP image processing functions. Which is the correct filter to apply?

Note: I'm from a photoshop background, so in the event my interpretation of "hue" is different from others, here's what I mean...

In photoshop, you can use the "Hue" filter to change the color of an image without actually affecting the design of the image.

I'm currently using the function below to recolor my images, but the function fails to meet my needs due to the fact that it completely repaints the image, losing the design.

 function set_theme_color_header($hex)
 {
  $info = hexToRGB($hex); //utility function that converts hex to rgb
  $token = "header.gif";
  $img = imagecreatefromgif("header-template.gif";
  $color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]);
  imagecolorset($img, 0, $info["red"], $info["green"], $info["blue"]);
  imagegif($img, $token);
 }
 ?>
+1  A: 
Tatu Ulmanen
Wow! Took you long enough! :-)Thanks a ton Tatu.
Scott B
Tatu, one thing I'm missing, and perhaps this is not possible, but, let's say, I just want to change the hue of the image from gray (the default) to something very close to #cc0000. Is this possible? Since I can't supply a hex in your function, How do I define the angle needed to achieve that?
Scott B
Also, in your example you start with a png, then create a jpeg from that. Will this work if I use an indexed gif for each step instead?
Scott B
If you have a gray image, changing the hue will not do anything (as the saturation is 0, any hue you give will result in a gray image). You might want to look at `imagefilter` and especially its `IMG_FILTER_COLORIZE` filter. And yes, you can output any filetype you want, so change the `imagejpeg` to `imagegif` for example.
Tatu Ulmanen
A: 

You'd normally convert from RGB to HSL color space, at which point you can manipulate the hue directly. Once you've done what you want, you convert back to RGB.

Jerry Coffin
A: 

Wow, great timing! I was looking for something quite similar to this and here it is just posted today! I am having a problem though, with transparent PNG images, and I think they need a modification for the alpha channel. If I figure it out, I will try to post here again.

underdog
A: 

i was searching for this. thanks

Muhammed K K