首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将png图像转换为jpeg,并使alpha默认为白色而非黑色?

如何将png图像转换为jpeg,并使alpha默认为白色而非黑色?
EN

Stack Overflow用户
提问于 2018-02-24 01:20:13
回答 1查看 0关注 0票数 0

我试了这样的,但它只是使图像的背景变白,而不一定是使图像alpha。我想只是上传所有的JPG格式,所以如果我能以某种方式“透明地”压缩一个PNG图像,默认它只是白色,所以我可以用它作为JPG。

代码语言:javascript
复制
$ old = imagecreatefrompng($ upload);
$ background = imagecolorallocate($ old,255,255,255);
imagefill($ old,0,0,$ background);
imagealphablending($ old,false);
imagesavealpha($ old,true);
EN

回答 1

Stack Overflow用户

发布于 2018-02-24 10:36:30

代码语言:txt
复制
<?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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档