将PNG转换为JPG,并使用ImageMagick和PHP将透明背景设置为白色 [英] Convert PNG to JPG and set transparent background to white with ImageMagick and PHP

查看:114
本文介绍了将PNG转换为JPG,并使用ImageMagick和PHP将透明背景设置为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将图像从PNG转换为JPEG时,如何使用ImageMagick(使用php扩展名)将透明背景设置为白色?

How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?

推荐答案

在撰写本文时,您尚未指定使用的扩展名,但如果您使用的是命令行,则命令为:

At time of writing, you have not specified which extension you are using, but if you were using the commandline, the command would be:

convert image.png -background white -flatten -alpha off image.jpg

更多信息可在屏蔽使用文档中找到。

比如使用IMagick,我认为你可以这样做:

Using IMagick for instance, I think you could do this as follows:

(完全未经测试,从未使用过IMagick而不是将它安装到测试中)

$image = new IMagick('image.png');

$flattened = new IMagick();
$flattened->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel("white"));

$flattened->compositeImage($image, imagick::COMPOSITE_OVER, 0, 0);

$flattened->setImageFormat("jpg");
$flattened->writeImage('image.jpg');

$image->clear();
$image->destroy();
$flattened->clear();
$flattened->destroy();

这篇关于将PNG转换为JPG,并使用ImageMagick和PHP将透明背景设置为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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