PHP - 使用imagecopy函数垂直合并两个图像

I have to merge two PNG images vertically.

//place at right side of $img1
imagecopy($merged_image, $img2, $img1_width, 0, 0, 0, $img2_width, $img2_height);

But I need to merge the images one after bottom one.(vertically)

How to merge that second image after bottom of first image?

Please advice!

PHP Code

$img1_path = 'images/1.png';
$img2_path = 'images/2.png';

list($img1_width, $img1_height) = getimagesize($img1_path);
list($img2_width, $img2_height) = getimagesize($img2_path);

$merged_width  = $img1_width + $img2_width;
//get highest
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height;

$merged_image = imagecreatetruecolor($merged_width, $merged_height);

imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

$img1 = imagecreatefrompng($img1_path);
$img2 = imagecreatefrompng($img2_path);

imagecopy($merged_image, $img1, 0, 0, 0, 0, $img1_width, $img1_height);
//place at right side of $img1
imagecopy($merged_image, $img2, $img1_width, 0, 0, 0, $img2_width, $img2_height);

//save file or output to broswer
$SAVE_AS_FILE = TRUE;
if( $SAVE_AS_FILE ){
    $save_path = "images/Sample_Output.png";
    imagepng($merged_image,$save_path);
}else{
    header('Content-Type: image/png');
    imagepng($merged_image);
}

//release memory
imagedestroy($merged_image);

Output:

enter image description here

When I consider the function

imagecopy ( resource $dst_im , resource$src_im , int $dst_x , int $dst_y , int $src_x , int$src_y , int $src_w , int $src_h )

It says that the dst_x and dst_y should be the x-coordinate of destination point and y-coordinate of destination point respectively.

So to merge two images vertically, it should be something like this.

imagecopy($merged_image, $img2, 0,$img1_height , 0, 0, $img2_width, $img2_height);

You should also change the $merged_width and $merged_height variable values as needed for the final result. Change the following two lines,

$merged_width = $img1_width + $img2_width; //get highest 
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height; 

As follows,

$merged_width = $img1_width > $img2_width ? $img1_width : $img2_width; //get highest width as result image width
$merged_height = $img1_height + $img2_height;

Final Result:

$img1_path = 'images/1.png';
$img2_path = 'images/2.png';

list($img1_width, $img1_height) = getimagesize($img1_path);
list($img2_width, $img2_height) = getimagesize($img2_path);

$merged_width = $img1_width > $img2_width ? $img1_width : $img2_width; //get highest width as result image width
$merged_height = $img1_height + $img2_height;

$merged_image = imagecreatetruecolor($merged_width, $merged_height);

imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

$img1 = imagecreatefrompng($img1_path);
$img2 = imagecreatefrompng($img2_path);

imagecopy($merged_image, $img1, 0, 0, 0, 0, $img1_width, $img1_height);
//place at right side of $img1
imagecopy($merged_image, $img2, 0,$img1_height , 0, 0, $img2_width, $img2_height);

//save file or output to broswer
$SAVE_AS_FILE = TRUE;
if( $SAVE_AS_FILE ){
    $save_path = "images/Sample_Output.png";
    imagepng($merged_image,$save_path);
}else{
    header('Content-Type: image/png');
    imagepng($merged_image);
}

//release memory
imagedestroy($merged_image);