PHP调整大小时优化PNG

PHP调整大小时优化PNG

本文介绍了使用GD / PHP调整大小时优化PNG - 如何确定调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在调整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: 原始图片 使用上面给出的解决方案调整图片大小 调整后的图片,带有tweak²Original ImageResized Image with solution given aboveResized Image with a tweak²²我在这里找到的调整的想法 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 imageimagecopyresampled($newImageTmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);// create output image$newImage = imagecreate($newwidth,$newheight);// copy resized truecolor image onto index-color imageimagecopy($newImage,$newImageTmp,0,0,0,0,$newwidth,$newheight);// write image to buffer and save in variableob_start(); // stdout --> bufferimagepng($newImage,NULL,6);$newImageToSave = ob_get_contents(); // store stdout in $newImageToSaveob_end_clean(); // clear buffer// remove images from php bufferimagedestroy($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 - 如何确定调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 04:56