我正在使用很多小图片创建一个新图片,让我给你看一个例子:
那些图像(背景为紫色的图像):
成为这一个:
问题是,使用下面的代码,我永远不会改变我合并的图像的大小,所以我真的不知道为什么图像被调整大小而我不要求它...
我尝试更改复合运算符See documentation但没有任何改变,总是出现同样的问题:原始图像在合并之前会调整大小。
这样做之后:
public static function mergeImage($layers, $filename) {
$images = [];
// Load all images
foreach ($layers as $layer) {
if (isset($layer['filename']) && isset($layer['x']) && isset($layer['y'])) {
$image = new stdClass();
$image->imagick = new Imagick($layer['filename']);
if ($image->imagick) {
$image->x = $layer['x'];
$image->y = $layer['y'];
$size = $image->imagick->getImageGeometry();
$image->width = $size['width'];
$image->height = $size['height'];
$images[] = $image;
}
}
}
if (count($images) > 0) {
// Define final image size
$imgWidth = 0;
$imgHeight = 0;
foreach ($images as $image) {
$imgWidth = max($imgWidth, $image->x + $image->width);
$imgHeight = max($imgHeight, $image->y + $image->height);
}
if ($imgWidth > 0 && $imgHeight > 0) {
// Create empty image
$merge = new Imagick();
$merge->newimage($imgWidth, $imgHeight, new ImagickPixel('none'));
// Add all images
foreach ($images as $image) {
$merge->compositeimage($image->imagick, Imagick::COMPOSITE_DEFAULT, $image->x, $image->y);
}
$merge->flattenimages();
// Write new image.
if (!$merge->writeimage($filename)) {
$result = -3;
}
} else {
$result = -2;
}
} else {
$result = -1;
}
$result = 1; // All went good
return $result;
}
事实上,这与所有测试完美配合,此测试唯一的问题是Imagick正在填充空白区域,其中拉伸图像会在其他测试中留下空白区域。
感谢您的帮助:)