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

发布于 2024-10-08 17:52:19 字数 2015 浏览 0 评论 0原文

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

这里是:

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/< /a>

双击任意图标,将出现弹出窗口。 (注意:在任何图标上双击都会显示相同的图像(我还没有修复这个问题)

知道如何使黑色像素看起来像带有 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 颜色并显示它时。有人看到那里有错误吗?

I read a couple of thing about this, but I can't figure how to put it in my code.

Here it is :

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;

}

If oyu want to see what it looks like, clic here: http://narks.xtreemhost.com/

And double-click on any icon, popup will appear. (Note: dbl-clinking on any icon will show the same image (I didnt fixed this yet)

Any idea how i can make the black pixel appear like the real pixel with alpha) ?

Thanks for your help!

EDITED
(New code, i put only the first lines since i want to save space)

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);
[...]

To see what it looks like now, visit the website above and double-click on an icon.

EDIT 2
I just tried (for a test) with :

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

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

and then with

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

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

The first is okay and the second make pixels black. So it is when i get each pixel's RGB colors and when I display it. Anyone see a mistake there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-10-15 17:52:19

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

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;

}

我希望它对某人有用

Here is the correct working code, in order to complete this question.

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;

}

I hope it will be useful for someone

无所谓啦 2024-10-15 17:52:19

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

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

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

According to comments on the imagecreatefrompng page, you need to call imagealphablending and imagesavealpha

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

There are also other comments about alpha transparency and PNGs on that page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文