php - Imagemagick - 你如何缩放图像 A 以适应图像 B 的某个部分?
问题描述
从image B
下面的输入开始,如何在保留透明文本的同时将输入的大小调整image A
为右侧?image B
image 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
粉色代表透明度的地方:
解决方案
推荐阅读
- cookiecutter-django - 部署 django-cookiecutter 项目所需的最少 AWS 要求/更改数量是多少?
- discord.py - 如何为每个服务器设置其独特的欢迎频道?不和谐.py
- c# - 为什么 Strings.Asc('š') 和 Strings.AscW('š') 返回不同的值?
- php - 用php替换文件中的字符
- python - 麻烦在python中反转矩阵
- django - Django Rest Framework - React - 注销后无法立即登录
- r - 在r中重新排序数据框中的行
- python - 如何编写程序刚刚生成的python文件?
- json - 从 ASMX Web 服务获取 JSON 列名
- android - 无法在谷歌私人商店中发布私人安卓应用