There are two graphic files. First file - JPG background image

The second file is a PNG with a figure in the center with a black border around the contour.

Question: How to combine two files with transparency preservation? The background from the first file should be placed inside the shape on the second file (see an example).

Example

Images:

PNG file - profiles.in.ua/tmp/sample2.jpg

JPG file - profiles.in.ua/tmp/sample1.png

I try to do this:

     $ mask = new Imagick (realpath ('mask.png'));

     $ pattern = new Imagick (realpath ('pattern.jpg'));

     $ pattern-> resizeImage ($ mask-> width, $ mask-> height, Imagick :: FILTER_LANCZOS, 1);

     $ pattern-> compositeImage ($ mask, Imagick :: COMPOSITE_ATOP, 0, 0);

     header ("Content-Type: image / png");

     echo $ pattern-> getImageBlob ();

     $ mask-> destroy ();

     $ pattern-> destroy ();

  • one
    I vote for the closure of this issue as not relevant topic, because it is a duplicate - tutankhamun
  • Please do not ask the same question multiple times. If in doubt, read the Help. How to ask questions . - Nick Volynkin
  • Since when do duplicates close like this? - Qwertiy
  • @Qwertiy in cases where the duplicate question does not contain plus answers, it is not possible to declare the question as “main” and indicate duplicates to it. Often this means that both questions are equally useless, although anything can happen .-. Here you can do more in the opposite direction, if I understand the mechanism correctly, but there is no reference to a duplicate to make sure. - D-side

1 answer 1

I used something like this, maybe it will help you

$img_path = "img1.jpg"; //передаем скрипту изображение, на которое нужно что-то наложить, в формате .jpg $watermark = "watermark.png"; //тут хранится путь к накладываемому изображению, в формате .png $img = imagecreatefromjpeg($img_path); //создаем исходное изображение $water_img = imagecreatefrompng($watermark); //создаем водный знак $water_size = getimagesize($water_img); //узнаем размеры водного знака, чтобы правильно выполнить наложение imagecopy($img, $water_img, 0, 0, 0, 0, $water_size[0], $water_size[1]); //накладываем водный знак на изображение по заданным координатам. header("Content-type: image/jpeg"); imagejpeg($img, $water_img , 100);