1

I have a website screenshot which is fixed width and height say 600x400px and now i want this image to merge with other image which has desktop screen on it. Now that screen is not exactly a square or rectangle that means my image need to fit the desktop of the screen. Simply i want to fill my screenshot image in (x1,x2) (y1,y2) coordinates.

enter image description here

What i need is black image needs to fit in the black screen area of Laptop.

I searched Imagick but not sure which function to use. For reference i'm trying to do something like placeit.net

Please help..

Praveen Govind
  • 2,619
  • 2
  • 32
  • 45

1 Answers1

3

Technically, you need to use an affine perspective transformation to convert the image you want to superimpose into the background image. That is a bit hard.

Luckily there is a simpler way in Image Magick where you just define 4 points in a source image space, and then 4 points in the final image space, and Image Magick does all the calculations for you.

Using the image you provided and the code:

<?php

$overlay = new Imagick(realpath("../images/overlay.jpg"));
$imagick = new Imagick(realpath("../images/Screeny.png"));

$overlay->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);

$width = $overlay->getImageWidth();
$height = $overlay->getImageHeight();

$points = array(
    0, 0,              364, 51,   
    $width, 0,         473.4, 23, 
    0, $height,         433.5, 182,  
    $width, $height,    523, 119.4 
);

$overlay->modulateImage(97, 100, 0 );
$overlay->distortImage(Imagick::DISTORTION_PERSPECTIVE, $points, TRUE );

//The offsets should be the minimum of the x and y values
$imagick->compositeImage($overlay, Imagick::COMPOSITE_OVER, 364.5, 23.5);

header("Content-Type: image/png");
echo $imagick->getImageBlob();

produces an image very much like:

picture embedded in other image

btw I would recommend doing this at a higher resolution than required, and then down-sampling, as well as applying a transparent blur to the overlay image, to try and make it look more realistic.

Danack
  • 24,939
  • 16
  • 90
  • 122