在PHP中在另一个底部添加一个图像

bak*_*lap 2 php gd

我想在php中添加一个图像到另一个底部

我这是加载图像:

//load top
$top = @imagecreatefrompng($templateTop);
//load bottom
$bottom = @imagecreatefrompng($templateBottom);
Run Code Online (Sandbox Code Playgroud)

现在我想将它们添加到一张图片中并一起显示顶部和底部.

我该怎么办?

谢谢!

net*_*der 17

使用imagecopy:

$top_file = 'image1.png';
$bottom_file = 'image2.png';

$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);

// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);

// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;

// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);

// save to file
imagepng($new, 'merged_image.png');
Run Code Online (Sandbox Code Playgroud)