Brij Sharma
Brij Sharma

Reputation: 371

How to merge two image in Laravel

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

Upvotes: 1

Views: 4866

Answers (2)

Brij Sharma
Brij Sharma

Reputation: 371

**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');

Upvotes: 0

user10461621
user10461621

Reputation:

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);

Upvotes: 2

Related Questions