skip to Main Content

to keep it simple I am trying to put a image on a image/canvas.
Canvas = 1920×1080, 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.

2

Answers


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

    enter image description here

    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
    

    enter image description here

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

    Login or Signup to reply.
  2. 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:

    enter image description here

    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:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search