【问题标题】:color error in converting a transparent png to jpg in php with glib使用glib在php中将透明png转换为jpg时出现颜色错误
【发布时间】:2017-04-26 13:00:47
【问题描述】:

我尝试使用 gdlib 将部分透明的 png 转换为 php 中的 jpg。我找到了两个 sn-ps 来帮助我解决这个问题,但两种方法都有相同的问题:半透明颜色更暗,看起来不正确。这是来自 Photoshop 的放大示例:左边是 png(背景为白色而不是透明),右边是我使用的两个 sn-ps 转换为 jpg 的 png:

difference png (left) to jpg (right)

这里是原始的 PNG 文件: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);

【问题讨论】:

    标签: php png jpeg gdlib


    【解决方案1】:

    您正在遭受量化的困扰。 JPEG 根本不能很好地处理这种类型的图像。如果您想减少颜色变化,您需要调整量化表。如果您对量化表使用全 1,您不会得到颜色变化。

    【讨论】:

    • 非常感谢您的回答。问题:如何影响php中的量化表?从来没有听说过,但我愿意做任何事情来解决这个问题......
    • 是否有某种“质量”设置?
    • 是的,如果您查看倒数第二行 (imagejpeg),最后一个参数是 100,表示质量最好。
    • 我会得到一个 JPEG 转储程序,看看量化表是否全为 1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2011-11-28
    相关资源
    最近更新 更多