在PHP中的图像转换

huangapple go评论61阅读模式
英文:

Image transform in PHP

问题

为了简化,我试图将一张图片放在另一张图片/画布上。
画布大小为1920x1080,图片大小不重要。
我想将图片添加到画布上,但要为每个角指定特定的x/y坐标。
看看下面的数组,了解我正在使用的内容:

[0] => Array
(
    [cp1x] => 500
    [cp1y] => 250
    [cp2x] => 250
    [cp2y] => 380
    [cp3x] => 400
    [cp3y] => 700
    [cp4x] => 1500
    [cp4y] => 800
)

CP1X/CP1Y是左上角。
CP2X/CP2Y是右上角。
CP3X/CP3Y是左下角。
CP4X/CP4Y是右下角。

我最好能得到一个使用PHP和/或ImageMagick的解决方案。

还制作了示意图以更好地解释。
希望一切都清楚。

图片

基础/画布

结果

尝试使用Imagick和PHP进行制作。

英文:

to keep it simple I am trying to put a image on a image/canvas.
Canvas = 1920x1080, image size doesn't matter.
I want to add the image into the canvas but with specific x/y coordination for each corner.
Look at my array here below to see what I'm working with:

[0] => Array
(
    [cp1x] => 500
    [cp1y] => 250
    [cp2x] => 250
    [cp2y] => 380
    [cp3x] => 400
    [cp3y] => 700
    [cp4x] => 1500
    [cp4y] => 800
)

CP1X/CP1Y are for the top left.
CP2X/CP2Y are for the top right.
CP3X/CP3Y are for the lower left.
CP4X/CP4Y are for the lower right.

I'd preferably get a solution in php and -or ImageMagick.

Also made sketches to give a better explanation.
Hope it's all clear.

Image

Base/canvas

Result

Tried making it with Imagick and PHP transform.

答案1

得分: 1

你的坐标与描述不匹配,它们按顺时针方向列出,从左上角开始。

你可以在Imagemagick中使用+distort perspective来获得在你的坐标和输入图像的角落之间的结果。

输入:

在PHP中的图像转换

Unix语法:

canvas_size="1920x1080"
canvas_color="blue"
# 从左上角开始列出的点
cp1x=500
cp1y=250
cp2x=250
cp2y=380
cp3x=400
cp3y=700
cp4x=1500
cp4y=800
infile="lena.png"
ww=`convert "$infile" -format "%w" info:`
hh=`convert "$infile" -format "%h" info:`
p1x=0
p1y=0
p2x=0
p2y=$((hh-1))
p3x=$((ww-1))
p3y=$((hh-1))
p4x=$((ww-1))
p4y=0
convert \( -size $canvas_size xc:"$canvas_color" \) \
\( "$infile" -virtual-pixel none -mattecolor none -define distort:viewport=${canvas_size}+0+0 +distort perspective \
"$p1x,$p1y $cp1x,$cp1y  $p2x,$p2y $cp2x,$cp2y  $p3x,$p3y $cp3x,$cp3y  $p4x,$p4y $cp4x,$cp4y" \) \
-compose over -composite \
result.jpg

结果:

在PHP中的图像转换

英文:

Your coordinates do not match your description. They are list counter clockwise from the top left.

You can get the result in Imagemagick using +distort perspective between your coordinates and the corners of the input images.

Input:

在PHP中的图像转换

Unix syntax:

canvas_size="1920x1080"
canvas_color="blue"
# points listed counter clockwise from top left
cp1x=500
cp1y=250
cp2x=250
cp2y=380
cp3x=400
cp3y=700
cp4x=1500
cp4y=800
infile="lena.png"
ww=`convert "$infile" -format "%w" info:`
hh=`convert "$infile" -format "%h" info:`
p1x=0
p1y=0
p2x=0
p2y=$((hh-1))
p3x=$((ww-1))
p3y=$((hh-1))
p4x=$((ww-1))
p4y=0
convert \( -size $canvas_size xc:"$canvas_color" \) \
\( "$infile" -virtual-pixel none -mattecolor none -define distort:viewport=${canvas_size}+0+0 +distort perspective \
"$p1x,$p1y $cp1x,$cp1y  $p2x,$p2y $cp2x,$cp2y  $p3x,$p3y $cp3x,$cp3y  $p4x,$p4y $cp4x,$cp4y" \) \
-compose over -composite \
result.jpg

Result:

在PHP中的图像转换

答案2

得分: 0

我没有任何可用的PHP设置,但以下是如何在命令行上执行的。我使用了一个1920x1080的漩涡作为背景(bg.jpg),将您的400x400叠加图像作为前景(fg.jpg)进行了扭曲:

在PHP中的图像转换

magick bg.jpg \
   \( fg.png -virtual-pixel transparent +distort Perspective '0,0,500,250 399,0 1500,380 0,399,400,800 399,399,1250,700' \) \
   -flatten result.png

在PHP中的图像转换

您将看到有4对坐标,源图像中的坐标在对应的目标图像点之前列出。

英文:

I don't have any working PHP setup, but here's how you do it on the command line. I used a 1920x1080 swirl as the background (bg.jpg) with your 400x400 overlay image as the foreground (fg.jpg) to distort:

在PHP中的图像转换

magick bg.jpg \
   \( fg.png -virtual-pixel transparent +distort Perspective '0,0,500,250 399,0 1500,380 0,399,400,800 399,399,1250,700' \) \
   -flatten result.png

在PHP中的图像转换

You will see there are 4 pairs of coordinates, with those in the source image listed before the corresponding point in the destination image.

huangapple
  • 本文由 发表于 2023年3月7日 23:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定