简体   繁体   English

使用glib在php中将透明png转换为jpg时出现颜色错误

[英]color error in converting a transparent png to jpg in php with glib

I try to convert a partly transparent png to a jpg in php with gdlib. 我尝试使用gdlib在php中将部分透明的png转换为jpg。 I found two snippets to help me with that, but both methods have the same problem: The half transparent colors are darker and do not look right. 我找到了两个片段来帮助解决这个问题,但是两种方法都存在相同的问题:半透明的颜色较暗,看起来不正确。 Here a enlarged sample from photoshop: left the png (with white in background instead of transparent), right the converted png to jpg with both snippets I used: 这里是来自Photoshop的放大示例:保留png(背景为白色,而不是透明的白色),将转换后的png转换为带有我使用的两个片段的jpg:

difference png (left) to jpg (right) png(左)与jpg(右)的差异

Here the original Png-File: golf.png 这是原始的Png档案: golf.png

Any help would be really appreciated! 任何帮助将非常感激!

$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);

You're suffering from quantization. 您正在遭受量化的困扰。 JPEG does not handle this type of image well at all. JPEG根本无法很好地处理此类图像。 If you want to lessen the color changes you need to adjust your quantization tables. 如果要减少颜色变化,则需要调整量化表。 If you use all 1s for the quantization tables you don't get the color changes. 如果将所有1都用于量化表,则不会发生颜色变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM