繁体   English   中英

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

[英]Pixel per Pixel image, transparent turned into black

我读了几本书,但是我不知道如何将其放入代码中。

这里是 :

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

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

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;

}

我希望这对某人有用

根据imagecreatefrompng页面上的注释,您需要调用imagealphablendingimagesavealpha

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM