用白色填充图像以具有正方形尺寸


Fill an Image with white color to have square dimensions

假设我有一个 300 x 500(宽 x 高)像素的图像。

现在我需要生成一个具有正方形尺寸的图像。

我想用白色填充缺失的空间,而不是裁剪图像。

在此示例中,with 应填充 200 像素(每边 100 像素,因此图像位于中间),以便图像为 500 x 500 像素。

如果相反(500 x 300),则应该发生相同的情况,但高度相同。

我几乎没有使用图像魔术的经验,所以我该怎么做呢?

使用 Imagick::borderImage()

左右各填充 100px 的情况示例:

// let's assume that $im is your image object
$color=new ImagickPixel();
$color->setColor("white");
$im->borderImage($color,100,0);