【发布时间】: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);
【问题讨论】: