像素每像素图像,透明变成黑色

时间:2010-12-18 18:44:57

标签: php png gd alpha

我读了几件关于此事的内容,但我无法弄清楚如何将其放入我的代码中。

这是:

function go($image) {
    // Open the image
    $getimage=imagecreatefrompng($image);
  //Get the width/height
   $w = imagesx($getimage);
   $h = imagesy($getimage);
   //init Count variable, when it reach the width, skip a line
   $count = 0;
   // For each line
   for($y=0;$y<$h;$y++) {
       // For each column
      for($x=0;$x<$w;$x++) {
        $rgba     = imagecolorat($getimage, $x, $y);
        $r = ($rgba >> 16) & 0xFF;
        $g = ($rgba >> 8) & 0xFF;
        $b = $rgba & 0xFF;
        $a     = ($rgba & 0x7F000000) >> 24;
        echo '<div class="pix" style="background-color: rgba('.$r.', '.$g.', '.$b.', '.$a.');"></div>';
        $count++;
        if($count==$w) {echo '<br>'; $count = 0; }

      }
   }
   echo $pixel_gen;

}

如果oyu希望看到它的样子,请点击此处:http://narks.xtreemhost.com/

双击任何图标,会出现弹出窗口。 (注意:任何图标上的dbl-clinking都会显示相同的图像(我还没有解决这个问题)

我知道如何让黑色像素看起来像带有alpha的真实像素?

感谢您的帮助!

EDITED (新代码,我只放了第一行,因为我想节省空间)

function go($image) {
    // Open the image
    $getimage=imagecreatefrompng($image);
    imagealphablending($getimage, false);
    imagesavealpha($getimage, true);

  //Get the width/height
   $w = imagesx($getimage);
   $h = imagesy($getimage);
[...]

要查看现在的样子,请访问上面的网站,然后双击图标。

编辑2 我刚试过(测试):

$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png');
imagealphablending($getimage, false);
imagesavealpha($getimage, true);

header("Content-type: image/png");
imagepng($getimage);
imagedestroy($getimage);

然后用

$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png');

header("Content-type: image/png");
imagepng($getimage);
imagedestroy($getimage);

第一个是好的,第二个使像素变黑。所以当我得到每个像素的RGB颜色并且当我显示它时。有人在那里看到错误吗?

2 个答案:

答案 0 :(得分:2)

这是正确的工作代码,以完成此问题。

function go($image) {
 // Open the image
 $getimage=imagecreatefrompng($image);
 imagealphablending($getimage, true);
 imagesavealpha($getimage, true);

  //Get the width/height
   $w = imagesx($getimage);
   $h = imagesy($getimage);
   //init Count variable, when it reach the width, skip a line
   $count = 0;
   // For each line
   for($y=0;$y<$h;$y++) {
    // For each column
      for($x=0;$x<$w;$x++) {
    // Get the image color for this pixel
  $rgba     = imagecolorat($getimage, $x, $y);
  $r = ($rgba >> 16) & 0xFF;
  $g = ($rgba >> 8) & 0xFF;
  $b = $rgba & 0xFF;
  $a = ($rgba & 0x7F000000) >> 24;
  //Calculating the correct Alpha value for rgba display in css
  $a = (127-$a)/127;
  echo '<div class="pix" style="background-color: rgba('.$r.', '.$g.', '.$b.', '.$a.');"></div>';
  $count++;
  if($count==$w) {echo '<br>'; $count = 0; }

      }
   }
   echo $pixel_gen;

}

我希望它对某人有用

答案 1 :(得分:1)

根据imagecreatefrompng页面上的评论,您需要致电imagealphablendingimagesavealpha

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

此页面上还有其他关于Alpha透明度和PNG的评论。

相关问题