dt2002 2018-04-26 12:22
浏览 87
已采纳

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

  • 写回答

1条回答 默认 最新

  • duaner5714 2018-04-26 14:07
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复