GD!将png图像转换为jpeg并使alpha默认为白色而非黑色

时间:2021-12-03 21:18:52

I tried something like this but it just makes the background of the image white, not necessarily the alpha of the image. I wanted to just upload everything as jpg's so if i could somehow "flatten" a png image with some transparently to default it to just be white so i can use it as a jpg instead. Appreciate any help. Thanks.

我试过这样的东西,但它只是使图像的背景变白,不一定是图像的alpha。我想只是将所有内容上传为jpg,所以如果我能以某种方式将某个png图像“透明化”并将其默认为白色,那么我可以将其用作jpg。感谢任何帮助。谢谢。

$old = imagecreatefrompng($upload);
$background = imagecolorallocate($old,255,255,255);
imagefill($old, 0, 0, $background);
imagealphablending($old, false);
imagesavealpha($old, true);

1 个解决方案

#1


53  

<?php
$input_file = "test.png";
$output_file = "test.jpg";

$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output,  255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);

#1


53  

<?php
$input_file = "test.png";
$output_file = "test.jpg";

$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output,  255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);