2017-04-18 50 views
10

ile oluşturulan PNG'den Alfa'yı alın PNG ürününün alpha sürümünü almaya çalışıyorum. Bunu imagepng() ile yapıyorum. Sorunum alpha döner sadece 0.imagepng()

alfa PNG yapmak için Kodum

$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 alfa okumak için Kodum

$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 

Nedenini bilmiyorum sadece 0 döndürür ve değil 1.

cevap

7

Siz imagesetpixel çağırmadan önce imageSaveAlpha ve imageSaveAlpha çağırır ould:

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

Güzel ile çözüm! teşekkür ederim. kolaydı :-) !!! –

1

Frank,

İşte

kodu

<!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>