使用GD / PHP调整大小时优化PNG - 如何确定调色板? [英] Optimize PNG when resizing with GD/PHP - How to determine color palette?

查看:107
本文介绍了使用GD / PHP调整大小时优化PNG - 如何确定调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调整PNG大小和维护小文件时遇到了麻烦。 此处的解决方案

I had troubles resizing a PNG and maintaining small file sizes. Solution found here.

然而,在调整PNG大小时,我遇到了有关图像质量的问题。据我所知,GD使用索引的8位颜色调色板,扭曲文本和颜色迷失,请参阅:

When resizing the PNG, however, I ran into problems regarding image quality. As far as I could see, GD uses indexed 8-bit-color palette which distorts text and colors get lost, see:

  • Original Image
  • Resized Image with solution given above
  • Resized Image with a tweak²

²我在这里找到的调整的想法< a href =https://stackoverflow.com/a/2650258/1066234> stackoverflow :创建truecolor-image,调整大小并将其复制到新图像,以便根据重新采样确定调色板结果和图像质量更好,您可以在上面的图片中看到。

²The idea for the tweak I found here in stackoverflow: Create truecolor-image, resize it, and copy it to a new image, so the palette is determined based on the resampled result and the image quality is better as you can see in the image above.

// create new image
$newImageTmp = imagecreatetruecolor($newwidth,$newheight);
// we create a temporary truecolor image
// do the image resizing by copying from the original into $newImageTmp image
imagecopyresampled($newImageTmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// create output image
$newImage = imagecreate($newwidth,$newheight);
// copy resized truecolor image onto index-color image
imagecopy($newImage,$newImageTmp,0,0,0,0,$newwidth,$newheight);
// write image to buffer and save in variable
ob_start(); // stdout --> buffer
imagepng($newImage,NULL,6);
$newImageToSave = ob_get_contents(); // store stdout in $newImageToSave
ob_end_clean(); // clear buffer
// remove images from php buffer
imagedestroy($src);
imagedestroy($newImageTmp);
imagedestroy($newImage);

问题:两个结果都不令人满意。

Problem: None of both results are satisfactory.

我很确定必须有一种方法来确定调色板,并且2.保持图像的大部分颜色,以便3. PNG看起来类似于原文并具有可接受的文件大小。

I am quite sure that there must be a way to 1. determine the color palette, and 2. maintain most of the image's colors, so that 3. the PNG looks similar to the original and has an acceptable file size.

现在,我只看到JPG而不是PNG。但如果您知道一个解决方案,如果您让我/我们知道,我们将不胜感激。

Now, I only see going for JPG instead of PNG. But if you know a solution, it would greatly be appreciated if you let me/us know.

谢谢!

推荐答案

所有你需要的是替换

 $newImage = imagecreate($newwidth,$newheight);

使用

 $newImage = imagecreatetruecolor($newwidth, $newheight);

输出 $ maxImgWidth = 200;

这篇关于使用GD / PHP调整大小时优化PNG - 如何确定调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆