2013-01-22 36 views
3

我試圖使用GD更改圖像的不透明度,幾乎所有解決方案都與下面的代碼類似,您在其中創建白色透明背景並將其與您的圖片。更改PHP中圖像的不透明度

但是這不會使圖像變得透明,圖像變得更亮,您實際上無法看透圖像。

所以我的問題是,如何更改圖像的不透明度,以便您可以查看它? 或者我在做這個代碼有問題嗎?

//Create an image with a white transparent background color 
$newImage = ImageCreateTruecolor(300, 300); 
$bg  = ImageColorAllocateAlpha($newImage, 255, 255, 255, 127); 
ImageFill($newImage, 0, 0, $bg); 

//Get the image 
$source = imagecreatefrompng('my_image.png'); 

$opacity = 50;  

//Merge the image with the background 
ImageCopyMerge($newImage, 
       $source, 
       0, 0, 0, 0, 
       300, 
       300, 
       $opacity); 

header('Content-Type: image/png'); 
imagepng($newImage); 
imagedestroy($newImage); 

謝謝!

+0

我不確定,但這只是一個不叫'imagealphablending'(或'imagesavealpha')的問題嗎? – abarnert

+0

經過快速搜索,可能dup:http://stackoverflow.com/questions/5529306/i-cant-use-transparent-background-with-imagecopymerge – abarnert

+0

你有沒有嘗試改變CSS的不透明度? – Nick

回答

4

使用DG功能沒有直接的方法來改變不透明度(實際上there is)。但它可以使用逐像素操作完成:

/** 
* @param resource $imageSrc Image resource. Not being modified. 
* @param float $opacity Opacity to set from 0 (fully transparent) to 1 (no change) 
* @return resource Transparent image resource 
*/ 
function imagesetopacity($imageSrc, $opacity) 
{ 
    $width = imagesx($imageSrc); 
    $height = imagesy($imageSrc); 

    // Duplicate image and convert to TrueColor 
    $imageDst = imagecreatetruecolor($width, $height); 
    imagealphablending($imageDst, false); 
    imagefill($imageDst, 0, 0, imagecolortransparent($imageDst)); 
    imagecopy($imageDst, $imageSrc, 0, 0, 0, 0, $width, $height); 

    // Set new opacity to each pixel 
    for ($x = 0; $x < $width; ++$x) 
     for ($y = 0; $y < $height; ++$y) { 
      $pixelColor = imagecolorat($imageDst, $x, $y); 
      $pixelOpacity = 127 - (($pixelColor >> 24) & 0xFF); 
      if ($pixelOpacity > 0) { 
       $pixelOpacity = $pixelOpacity * $opacity; 
       $pixelColor = ($pixelColor & 0xFFFFFF) | ((int)round(127 - $pixelOpacity) << 24); 
       imagesetpixel($imageDst, $x, $y, $pixelColor); 
      } 
     } 

    return $imageDst; 
} 

$source = imagecreatefrompng('my_image.png'); 
$newImage = imagesetopacity($source, 0.5); 
imagedestroy($source); 

header('Content-Type: image/png'); 
imagepng($newImage); 
imagedestroy($newImage); 
+0

acutally有。看看我的回答 – toraman

+0

@toraman是的,它真的有用。投票答覆。 – Finesse

+0

如果您有圓形圖像,那麼它會在新圖像中轉換爲方形。所以它不能正常工作。 –

0

您是否嘗試將alpha混合設置爲true?

imagealphablending($newImage,true);

7

你只需要使用imagefilterIMG_FILTER_COLORIZE

$image = imagecreatefrompng('my_image.png'); 
$opacity = 0.5; 
imagealphablending($image, false); // imagesavealpha can only be used by doing this for some reason 
imagesavealpha($image, true); // this one helps you keep the alpha. 
$transparency = 1 - $opacity; 
imagefilter($image, IMG_FILTER_COLORIZE, 0,0,0,127*$transparency); // the fourth parameter is alpha 
header('Content-type: image/png'); 
imagepng($image); 

imagealphablending,我認爲,是繪製的目的,所以你不想使用它。我可能是錯的。我們都應該查看它:)

如果你想使用百分比,你可以相應地計算$opacity

+0

實驗表明,'imagealphablending'和'imagesavealpha'值都不會影響透明和不透明圖像的'imagefilter'結果。也許他們會影響'imagepng'結果。 – Finesse