2017-04-18 64 views
10

我試圖獲得PNGalpha。我正在用imagepng()來做這件事。 我的問題是alpha只返回0。從imagepng創建的PNG中獲取Alpha

我製作PNG與阿爾法碼

$x = 1; 
$y = 1; 
$gd = imagecreatetruecolor($x, $y); 
imagesetpixel($gd, 0,0, imagecolorallocatealpha($gd, 200,200,200,1)); 
imageAlphaBlending($gd, false); 
imageSaveAlpha($gd, true); 
imagepng($gd,"test.png"); 
imagedestroy($gd); 

我的讀取PNG字母代碼

$im = imagecreatefrompng("test.png"); 
$rgb = imagecolorat($im, 0, 0); 
$colors = imagecolorsforindex($im, $rgb); 
$red = (int) $colors["red"]; 
$blue = (int) $colors["blue"]; 
$green = (int) $colors["green"]; 
$alpha = (int) $colors["alpha"]; // return only 0 

我不知道爲什麼它只返回0而不是1.

回答

7

你sh烏爾德打電話imageSaveAlphaimageSaveAlpha調用imagesetpixel前:

imageAlphaBlending($gd, false); 
imageSaveAlpha($gd, true); 
imagesetpixel($gd, 0,0, imagecolorallocatealpha($gd, 200,200,200,1)); 
+0

尼斯您的解決方案!謝謝。很容易 :-) !!! –

1

弗蘭克,

這裏是代碼

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title>Test</title> 

    </head> 
    <body> 

     <a href="index.php" class="navbar-brand"> 
      <?php 
      $x = 1; 
      $y = 1; 
      $gd = imagecreatetruecolor($x, $y); 
      imageAlphaBlending($gd, false); 
      imageSaveAlpha($gd, true); 
      imagesetpixel($gd, 0, 0, imagecolorallocatealpha($gd, 200, 200, 200, 1)); 
      imagepng($gd, 'img/logo.png" '); 
      imagedestroy($gd); 

      $im = imagecreatefrompng('img/logo.png'); 
      $rgb = imagecolorat($im, 0, 0); 
      $colors = imagecolorsforindex($im, $rgb); 
      $red = (int) $colors["red"]; 
      $blue = (int) $colors["blue"]; 
      $green = (int) $colors["green"]; 
      echo $alpha = (int) $colors["alpha"]; // return only 0 
      ?> 
     </a> 

    </body> 
</html>