1

I referred to answers on this question. And I am currently using following code for hue overlay:

function imagehue(&$image, $angle) {
if($angle % 360 == 0) return;
$width = imagesx($image);
$height = imagesy($image);

for($x = 0; $x < $width; $x++) {
    for($y = 0; $y < $height; $y++) {
        $rgb = imagecolorat($image, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;            
        $alpha = ($rgb & 0x7F000000) >> 24;
        list($h, $s, $l) = rgb2hsl($r, $g, $b);
        $h += $angle / 360;
        if($h > 1) $h--;
        list($r, $g, $b) = hsl2rgb($h, $s, $l);            
        imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha));
    }
  }
}

It works good with JPG. But this code doesn't work with transparent PNG images. This is how I call this function for PNG images:

header('Content-type: image/png');
**$image = imagecreatefrompng('image.png');**
imagehue($image, 180);
imagejpeg($image);

Does anyone know what changes I should make?

1 Answer 1

2

It's because you use the imagejpeg function, use imagepng instead. And if you also want it to work with alpha transparency, add this to your code:

imagealphablending($image, false);
imagesavealpha($image, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.