用GLIB将透明的PNG转换为JPG的颜色误差



我尝试将部分透明的PNG转换为使用GDLIB的PHP中的JPG。我找到了两个片段可以帮助我解决这个问题,但是两种方法都有相同的问题:半透明的颜色更深,看起来不正确。这里是Photoshop的放大样品:剩下PNG(在背景中使用白色,而不是透明),将PNG转换为JPG,并使用我使用的两个smippets:

差异PNG(左)与JPG(右)

在这里原始的png-file:golf.png

任何帮助都将不胜感激!

$input_file = "card/golf.png";
$output_file1 = "card/golf1.jpg";
$output_file2 = "card/golf2.jpg";
$image = imagecreatefrompng($input_file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagejpeg($bg, $output_file1, 100);
imagedestroy($bg);
imagedestroy($image);
list($width, $height) = getimagesize($input_file);
$image = imagecreatefrompng($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output,  255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $image, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file2, 100);
imagedestroy($output);

您患有量化。JPEG根本无法很好地处理此类型的图像。如果要降低颜色更改,则需要调整量化表。如果您将所有1s用于量化表,则无法更改颜色。

最新更新