如何在Laravel

时间:2018-10-06 07:18:54

标签: javascript php css laravel

我想使用Laravel将两个图像彼此合并。

第一张图片

First Image

第二张图片

enter image description here

我希望最终结果像

enter image description here

图片必须可下载

2 个答案:

答案 0 :(得分:2)

使用默认的php函数(imagecopymerge)(GD库):

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

对于 jpeg 格式,请使用打击功能:

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

如果您不想存储,而只想将其发送给用户:

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

// Output the image
imagejpeg($master);

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

答案 1 :(得分:0)

**此代码有效**

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