合并两张图片php,用PHP合并两个图像

您可以尝试使用我的功能来水平或垂直合并图像而不更改图像比率。只需复制粘贴即可。

function merge($filename_x, $filename_y, $filename_result, $mergeType = 0) {

//$mergeType 0 for horizandal merge 1 for vertical merge

// Get dimensions for specified images

list($width_x, $height_x) = getimagesize($filename_x);

list($width_y, $height_y) = getimagesize($filename_y);

$lowerFileName = strtolower($filename_x);

if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){

$image_x = imagecreatefromjpeg($filename_x);

}else if(substr_count($lowerFileName, '.png')>0){

$image_x = imagecreatefrompng($filename_x);

}else if(substr_count($lowerFileName, '.gif')>0){

$image_x = imagecreatefromgif($filename_x);

}

$lowerFileName = strtolower($filename_y);

if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){

$image_y = imagecreatefromjpeg($filename_y);

}else if(substr_count($lowerFileName, '.png')>0){

$image_y = imagecreatefrompng($filename_y);

}else if(substr_count($lowerFileName, '.gif')>0){

$image_y = imagecreatefromgif($filename_y);

}

if($mergeType==0){

//for horizandal merge

if($height_y

$new_height = $height_y;

$new_x_height = $new_height;

$precentageReduced = ($height_x - $new_height)/($height_x/100);

$new_x_width = ceil($width_x - (($width_x/100) * $precentageReduced));

$tmp = imagecreatetruecolor($new_x_width, $new_x_height);

imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);

$image_x = $tmp;

$height_x = $new_x_height;

$width_x = $new_x_width;

}else{

$new_height = $height_x;

$new_y_height = $new_height;

$precentageReduced = ($height_y - $new_height)/($height_y/100);

$new_y_width = ceil($width_y - (($width_y/100) * $precentageReduced));

$tmp = imagecreatetruecolor($new_y_width, $new_y_height);

imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);

$image_y = $tmp;

$height_y = $new_y_height;

$width_y = $new_y_width;

}

$new_width = $width_x + $width_y;

$image = imagecreatetruecolor($new_width, $new_height);

imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);

imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y);

}else{

//for verical merge

if($width_y

$new_width = $width_y;

$new_x_width = $new_width;

$precentageReduced = ($width_x - $new_width)/($width_x/100);

$new_x_height = ceil($height_x - (($height_x/100) * $precentageReduced));

$tmp = imagecreatetruecolor($new_x_width, $new_x_height);

imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);

$image_x = $tmp;

$width_x = $new_x_width;

$height_x = $new_x_height;

}else{

$new_width = $width_x;

$new_y_width = $new_width;

$precentageReduced = ($width_y - $new_width)/($width_y/100);

$new_y_height = ceil($height_y - (($height_y/100) * $precentageReduced));

$tmp = imagecreatetruecolor($new_y_width, $new_y_height);

imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);

$image_y = $tmp;

$width_y = $new_y_width;

$height_y = $new_y_height;

}

$new_height = $height_x + $height_y;

$image = imagecreatetruecolor($new_width, $new_height);

imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);

imagecopy($image, $image_y, 0, $height_x, 0, 0, $width_y, $height_y);

}

$lowerFileName = strtolower($filename_result);

if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){

imagejpeg($image, $filename_result);

}else if(substr_count($lowerFileName, '.png')>0){

imagepng($image, $filename_result);

}else if(substr_count($lowerFileName, '.gif')>0){

imagegif($image, $filename_result);

}

// Clean up

imagedestroy($image);

imagedestroy($image_x);

imagedestroy($image_y);

}

merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged_har.jpg',0); //merge horizontally

merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged.jpg',1); //merge vertically

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值