如何使用 PHP GD 库将 PNG 转换为 8 位 PNG

发布于 2024-11-02 13:02:41 字数 68 浏览 9 评论 0原文

我想编写一个例程,以 PNG 图像路径作为参数,并将该图像转换为 8 位 PNG 图像。我需要为此使用 PHP GD 库。

I want to write a routine which takes PNG image path as parameter and convert that image into 8-bit PNG image. I need to use PHP GD library for this.

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

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

发布评论

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

评论(2

明媚如初 2024-11-09 13:02:41

要将任何 PNG 图像转换为 8 位 PNG,请使用此函数,我刚刚创建了

函数 ConvertPNGto8bitPNG ()

 function convertPNGto8bitPNG ($sourcePath, $destPath) {

     $srcimage = imagecreatefrompng($sourcePath);
     list($width, $height) = getimagesize($sourcePath);

     $img = imagecreatetruecolor($width, $height);
     $bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
     imagecolortransparent($img, $bga);
     imagefill($img, 0, 0, $bga);
     imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height);
     imagetruecolortopalette($img, false, 255);
     imagesavealpha($img, true);

     imagepng($img, $destPath);
     imagedestroy($img);

 }

参数

  • $sourcePath - 源 PNG 文件的路径
  • $destPath - 路径到目标 PNG 文件

注意

我建议在运行此代码之前确保 $sourcePath 存在并且 $destPath 可写。也许此功能不适用于某些透明图像。

使用

convertPNGto8bitPNG ('pfc.png', 'pfc8bit.png');

示例(原始 -> 8 位)

(来源:pfc.png) 原始 PNG 图像

在此处输入图像描述

(目标:pfc8bit.png)转换后的 PNG 图像(8 位)

在此处输入图像描述

希望有人觉得这有帮助。

To convert any PNG image to 8-bit PNG use this function, I've just created

function convertPNGto8bitPNG ()

 function convertPNGto8bitPNG ($sourcePath, $destPath) {

     $srcimage = imagecreatefrompng($sourcePath);
     list($width, $height) = getimagesize($sourcePath);

     $img = imagecreatetruecolor($width, $height);
     $bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
     imagecolortransparent($img, $bga);
     imagefill($img, 0, 0, $bga);
     imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height);
     imagetruecolortopalette($img, false, 255);
     imagesavealpha($img, true);

     imagepng($img, $destPath);
     imagedestroy($img);

 }

Parameters

  • $sourcePath - Path to source PNG file
  • $destPath - Path to destination PNG file

Note

I recommend to make sure that $sourcePath exists and $destPath is writable before running this code. Maybe this function won't work with some transparent images.

Usage

convertPNGto8bitPNG ('pfc.png', 'pfc8bit.png');

Example (original -> 8-bit)

(Source: pfc.png) ORIGINAL PNG IMAGE

enter image description here

(Destination: pfc8bit.png) CONVERTED PNG IMAGE (8-bit)

enter image description here

Hope someone finds this helpful.

无语# 2024-11-09 13:02:41

我强烈建议使用 exec()使用 pngquant 1.5+ 命令行,而不是 GD 库popen() 函数。

GD 库的调色板生成代码质量非常差。

与其他答案中的图像相同,文件大小与 GD 库相同,但使用 pngquant 转换为仅 100 种颜色(甚至不是 256):

在此处输入图像描述

pngquant 很好地支持 alpha 透明度。

Instead of GD library I strongly recommend using pngquant 1.5+ commandline using exec() or popen() functions.

GD library has very poor-quality palette generation code.

Same image as in the other answer, same file size as GD library, but converted using pngquant to merely 100 colors (not even 256):

enter image description here

pngquant supports alpha transparency very well.

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