1

I want to merge two images one over another using Laravel.

First Image

First Image

Second image

enter image description here

And I want Final Result like

enter image description here

Image Must be downloadable

Brij Sharma
  • 361
  • 6
  • 19
  • Hello brij, I hope this github repo is useful for you. https://github.com/Intervention/image – Yogendrasinh Oct 06 '18 at 07:22
  • You need to use GD library or ImageMagic: https://stackoverflow.com/questions/23152108/php-place-an-image-over-another-image – somsgod Oct 06 '18 at 07:26

2 Answers2

2

use default php functions (imagecopymerge) ( GD library ):

<?php

list($new_width, $new_height, $new_type, $new_attr) = getimagesize("newimage.png");
switch(image_type_to_mime_type($new_type)){
     case IMAGETYPE_GIF:
         $new = imagecreatefrompng('newimage.png');
     break;
     case IMAGETYPE_JPEG:
         $new = imagecreatefromjpeg('newimage.png');
     break;
     case IMAGETYPE_PNG:
         $new = imagecreatefromgif('newimage.png');
     break;
}

$master = imagecreatefrompng('master.png');


imagealphablending($master, false);
imagesavealpha($demasterst, true);

imagecopymerge($master, $new, $box_x, $box_y, 0, 0, $box_w, $box_h, 100);
// imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

//save image
imagepng($master, "file.png");

imagedestroy($master);
imagedestroy($new);

for jpeg format use blow function:

  1. http://php.net/manual/en/function.imagejpeg.php
  2. http://php.net/manual/en/function.imagecreatefromjpeg.php

If you do not want to store it and just want to send it to the user:

...
...
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($master);

// Free up memory
imagedestroy($master);
imagedestroy($new);
0

**this code work **

<?php

$image1 = 'images/template.png';
$image2 = 'images/1.png';

list($width,$height) = getimagesize($image2);

$image1 = imagecreatefromstring(file_get_contents($image1));
$image2 = imagecreatefromstring(file_get_contents($image2));

imagecopymerge($image1,$image2,40,100,0,0,$width,$height,100);
header('Content-Type:image/png');
imagepng($image1);

imagepng($image1,'merged.png');
Brij Sharma
  • 361
  • 6
  • 19