2013-02-19 91 views
1

我提到this問題的答案。 目前我使用的色調覆蓋下面的代碼:使用PHP更改圖像(透明PNG)的色相GD

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)); 
    } 
    } 
} 

它的工作原理與良好JPG。但是這個代碼不適用於透明的PNG圖像。 這是我如何調用該函數PNG圖像:

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

有誰知道什麼樣的變化我應該做?

回答

2

這是因爲您使用imagejpeg函數,請改用imagepng。 如果您還希望它能夠使用alpha透明度,請將此添加到您的代碼中:

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