GD!将png图像转换为jpeg并使alpha默认为白色而非黑色 [英] GD! Converting a png image to jpeg and making the alpha by default white and not black

查看:108
本文介绍了GD!将png图像转换为jpeg并使alpha默认为白色而非黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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


推荐答案

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

这篇关于GD!将png图像转换为jpeg并使alpha默认为白色而非黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆