使用 Imagick 将图像放在白色背景上

Place Image On White Background Using Imagick

这是我打算做的 GD 版本。它将图像放在白色背景的中心。

$background = imagecreatetruecolor(709,709);
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground);
imagecopyresampled(
    $background, $new_img,(709-$imageWidth)/2,(709-$imageHeight)/2,
    0, 0, $imageWidth, $imageHeight, $width, $height
);
ImageJpeg ($background,"$path/$newName.$file_ext", 85);`

但是我想用 Imagick 来实现。

欢迎来到 SO,

你要找的是扁平化;

$im = new Imagick($old_img);
$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(11);
$im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);
$im->setFormat("png");

$new = fopen($path . DIRECTORY_SEPARATOR . $newName . DIRECTORY_SEPARATOR . $file_ext, "w");
$im->writeImageFile($new);
$im->clear();
$im->destroy();

并不是说这正是您要找的东西,但它应该对您有所帮助。 $im->setImageAlphaChannel(11); 如果你的 Imagic 版本是,可以用 $im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); 代替 3.2.0b2 或更高版本