dongzhui9936 2010-12-18 18:44
浏览 82
已采纳

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

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?

  • 写回答

2条回答 默认 最新

  • duancan5327 2010-12-21 00:54
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘