首页 > 解决方案 > Imagemagick - 你如何缩放图像 A 以适应图像 B 的某个部分?

问题描述

image B下面的输入开始,如何在保留透明文本的同时将输入的大小调整image A为右侧?image Bimage B

header("Content-Type: image/jpg");
$a_resized = resizeImagePath('imagea.jpg',32,128,imagick::FILTER_BOX,1,1,false);
$a_resized = resizeImage($a_resized,256,630,imagick::FILTER_GAUSSIAN,1,1,false);
// echo $img->getImageBlob(); // works

$b_raw = new Imagick(realpath('image0transparent.png'));
$b_raw2 = new Imagick(realpath('image0transparent.png'));

$a_resized->setImageGravity(imagick::GRAVITY_EAST);

$b_raw->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$b_raw->setImageArtifact('compose:args', "1,0,-0.5,0.5");
$b_raw->compositeImage($a_resized, Imagick::COMPOSITE_DEFAULT, 946, 0);
$b_raw->compositeImage($b_raw2, Imagick::COMPOSITE_DEFAULT, 0, 0);
echo $b_raw->getImageBlob();;///->writeImage("./output.png");

//https://legacy.imagemagick.org/Usage/blur/#blur_resize
// https://www.php.net/manual/en/imagick.resizeimage.php
function resizeImagePath($imagePath, $width, $height, $filterType, $blur, $bestFit, $cropZoom) {
    return resizeImage(new Imagick(realpath($imagePath)), $width, $height, $filterType, $blur, $bestFit, $cropZoom);
}
 function resizeImage($imagick, $width, $height, $filterType, $blur, $bestFit, $cropZoom) {
    //The blur factor where > 1 is blurry, < 1 is sharp. 
    $w = $imagick->getImageWidth();
    $h = $imagick->getImageHeight();
    $width = $w/$h*$height;

    $imagick->resizeImage($width, $height, $filterType, $blur, $bestFit);

    $cropWidth = $imagick->getImageWidth();
    $cropHeight = $imagick->getImageHeight();

    if ($cropZoom) {
        $newWidth = $cropWidth / 2;
        $newHeight = $cropHeight / 2;

        $imagick->cropimage(
            $newWidth,
            $newHeight,
            ($cropWidth - $newWidth) / 2,
            ($cropHeight - $newHeight) / 2
        );

        $imagick->scaleimage(
            $imagick->getImageWidth() * 4,
            $imagick->getImageHeight() * 4
        );
    }
 
    return $imagick; 
}

图像 A

在此处输入图像描述

图像 B(右侧部分是透明的,带有文本覆盖):

在此处输入图像描述

期望的结果:图像 A 超过 B:

在此处输入图像描述

这是image B粉色代表透明度的地方:

在此处输入图像描述

标签: phpimagemagick

解决方案


以下是如何在 ImageMagick 命令行中执行此操作。你可以用 PHP Imagick 做同样的事情。

将图像 A 调整到图像 B 的高度。然后使用 compose 方法 dstOver 将调整后的图像 A 合成到图像 B 的右侧)。

请出示您的代码,以便我们进一步提供帮助。

一个.png

在此处输入图像描述

B.png

在此处输入图像描述

convert B.png \( A.png -resize x630 \) -gravity east -compose dstOver -composite x.png

在此处输入图像描述


推荐阅读