php 图片合成

27 篇文章 1 订阅
3 篇文章 0 订阅

/**
 * 图片合成
 * */
function composeImg($bigImgPath,$qCodePath,$dst_x,$dst_y){

    $bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
    $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
    imagesavealpha($bigImg,true);//假如是透明PNG图片,这里很重要 意思是不要丢了图像的透明<code class="php spaces hljs"></code>
    list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
    // imagecopymerge使用注解
    imagecopymerge($bigImg, $qCodeImg, $dst_x, $dst_y, 0, 0, $qCodeWidth, $qCodeHight, 100);
    list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);

    switch ($bigType) {
        case 1: //gif
            imagegif($bigImg,$bigImgPath);
            break;
        case 2: //jpg
            imagejpeg($bigImg,$bigImgPath);
            break;
        case 3: //jpg
            imagepng($bigImg,$bigImgPath);  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
            break;
        default:
            # code...
            break;
    }
    imagedestroy($bigImg);
    imagedestroy($qCodeImg);
}

 

/*背景合成*/
        //$back_img  http链接
        //$wx_code   http链接

        $dir_root = './uploads/uid2';
        $first_dir = 'wechatcode/recruit_agent';
        USER_ID = 1;

        $image_1 = imagecreatefrompng($back_img);
        $img_type = getimagesize ($wx_code)['mime'];
        if($img_type=='image/png'){
            $image_2 = imagecreatefrompng($wx_code);
        }else{
            $image_2 = imagecreatefromjpeg($wx_code);
        }
        //合成图片
        //imagecopymerge ($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,x$src_w,$src_h,$pct)---拷贝并合并图像的一部分
        //将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
        imagecopymerge($image_1, $image_2, 206, 463, 0, 0, imagesx($image_2), 
        imagesy($image_2), 100);
        // 输出合成图片
        //imagepng($image[,$filename]) — 以 PNG 格式将图像输出到浏览器或文件
        if(!file_exists($dir_root.'/'.$first_dir.'/'.USER_ID.'.png')){
            createFile($dir_root.'/'.$first_dir.'/'.USER_ID.'.png');
        }
        imagepng($image_1,$dir_root.'/'.$first_dir.'/'.USER_ID.'.png');

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昊喵喵博士

大哥你真帅,小姐姐你真漂亮

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值