How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?
在将图像从PNG转换为JPEG时,如何使用ImageMagick(使用php扩展名)将透明背景设置为白色?
2 个解决方案
#1
20
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
More information can be found on the Masking Usage documentation.
可以在Masking Usage文档中找到更多信息。
Using IMagick for instance, I think you could do this as follows:
以IMagick为例,我认为你可以这样做:
(totally untested, never used IMagick and don't have it installed to test)
(完全未经测试,从未使用过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();
#2
5
If you are using the Imagick extension:
如果您使用的是Imagick扩展程序:
<?php
// load the source transparent png
$i = new IMagick('image.png');
// set the background to white
// you can also use 'rgb(255,255,255)' in place of 'white'
$i->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$i = $i->flattenImages();
// the output format
$i->setImageFormat('jpg');
// save to disk
$i->writeImage('image.jpg');
// and/or output directly
// header('Content-Type: '.$i->getFormat());
// echo $i->getImageBlob();
// cleanup
$i->clear();
$i->destroy();
#1
20
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
More information can be found on the Masking Usage documentation.
可以在Masking Usage文档中找到更多信息。
Using IMagick for instance, I think you could do this as follows:
以IMagick为例,我认为你可以这样做:
(totally untested, never used IMagick and don't have it installed to test)
(完全未经测试,从未使用过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();
#2
5
If you are using the Imagick extension:
如果您使用的是Imagick扩展程序:
<?php
// load the source transparent png
$i = new IMagick('image.png');
// set the background to white
// you can also use 'rgb(255,255,255)' in place of 'white'
$i->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$i = $i->flattenImages();
// the output format
$i->setImageFormat('jpg');
// save to disk
$i->writeImage('image.jpg');
// and/or output directly
// header('Content-Type: '.$i->getFormat());
// echo $i->getImageBlob();
// cleanup
$i->clear();
$i->destroy();