【问题标题】:Pixel per Pixel image, transparent turned into blackPixel per Pixel 图像,透明变成黑色
【发布时间】:2011-05-27 15:23:22
【问题描述】:

我阅读了一些关于此的内容,但我不知道如何将其放入我的代码中。

这里是:

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 的真实像素吗?

感谢您的帮助!

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

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 颜色并显示它时。有人看到那里有错误吗?

【问题讨论】:

    标签: php png gd alpha


    【解决方案1】:

    根据imagecreatefrompng页面上的cmets,需要调用imagealphablendingimagesavealpha

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

    该页面上还有其他关于 alpha 透明度和 PNG 的 cmets。

    【讨论】:

    • 它不起作用。有关我的代码,请参阅已编辑的原始帖子。您可以在此处查看 (narks.xtreemhost.com) 并双击一个图标。
    【解决方案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;
    
    }
    

    我希望它对某人有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-25
      • 2013-08-21
      • 1970-01-01
      • 2017-06-04
      • 2013-11-25
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多