【问题标题】:PHP gdLib 8-Bit PNG with alphaPHP gdLib 8 位 PNG 带 alpha
【发布时间】:2010-12-18 05:35:36
【问题描述】:

如何将我用 gd 创建的图像保存为 png-8?

它可以很好地保存为带有透明通道的 gif - 但我想使用 png-8。

最好的问候, 啤酒韦斯尔

【问题讨论】:

    标签: php png gd png-8


    【解决方案1】:

    【讨论】:

    • 并非如此。这实际上就是我对 imagegif 所做的 - 但如果我使用 imagepng,它是一个 24 位图像
    • AFAIK,具有 alpha 透明度的 PNG 必须为 24 位。对于 8 位 PNG,您必须从调色板中选择一种透明的颜色。
    【解决方案2】:
    <?php
    // Create a new true color image
    $im = new imagecreatetruecolor(100, 100);
    
    // Convert to palette-based with no dithering and 255 colors
    imagetruecolortopalette($im, false, 255);
    
    // Save the image
    imagepng($im, './paletteimage.png');
    imagedestroy($im);
    ?>
    

    这应该是 8bit png

    【讨论】:

    • 如果我这样做,透明度就会消失
    【解决方案3】:

    基于 dfilkovi 的解决方案,您是否尝试过使用imagesavealpha() 保存完整的 alpha 通道信息?

    【讨论】:

      【解决方案4】:

      使用imagesavealpha() 和透明的背景颜色应该可以解决问题...

      基于 dfilkovi 的代码:

      <?php
      // Create a new true color image
      $im = new imagecreatetruecolor(100, 100);
      
      // Fill with alpha background
      $alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
      imagefill($im, 0, 0, $alphabg);
      
      // Convert to palette-based with no dithering and 255 colors with alpha
      imagetruecolortopalette($im, false, 255);
      imagesavealpha($im, true);
      
      // Save the image
      imagepng($im, './paletteimage.png');
      imagedestroy($im);
      ?>
      

      【讨论】:

        【解决方案5】:

        @桑尼

        错误假设:任何位深度的 PNG 都可以具有透明度。它记录在png图像的tRNS块中(真彩色除外)cf格式定义

        cf www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.tRNS

        同上 www.w3.org/TR/PNG-Chunks.html#C.tRNS

        不同之处在于它是如何记录的:RGBA 每个像素有一个唯一的记录,有 4 个值(3 种颜色和 1 个 alpha 通道),其中“调色板”PNG 在其自己的块中记录 alpha 通道。

        烟花很擅长。

        例子:

        http://www.libpng.org/pub/png/pngs-img.html

        【讨论】:

          【解决方案6】:

          我必须添加行 imagecolortransparent($im, $alphabg);到以下代码(取自先前的答案)以使其正常工作:

          // Fill with alpha background
          $alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
          imagecolortransparent($im, $alphabg);
          imagefill($im, 0, 0, $alphabg);
          
          // Convert to palette-based with no dithering and 255 colors with alpha
          imagetruecolortopalette($im, false, 255);
          imagesavealpha($im, true);
          
          // Save the image
          imagepng($im, './paletteimage.png');
          imagedestroy($im);
          ?>
          

          【讨论】:

            猜你喜欢
            • 2013-04-11
            • 2016-07-27
            • 2014-02-02
            • 1970-01-01
            • 2012-09-16
            • 2015-03-26
            • 1970-01-01
            • 2012-11-14
            • 1970-01-01
            相关资源
            最近更新 更多