GD添加文字+缩略图合成一张图

$getNotes = $this->getFeeNotes($data,$type); //获取文字的拼接字段

            $bgimg = public_path('images').'/bg.png';//背景图本地路径

            //文字拼接
            $str = '承诺函';
            $str2 = '乐刷产品费用说明';
            $str3 = '本人'.$getNotes['data']['owner_name'].'已充分知悉将使用乐刷POS产品';
            $str4 = '1、激活服务费:机具激活时支付'.$getNotes['data']['deposit'].'元';
            $str5 = '2、通讯服务费:入网'.$getNotes['data']['days'].'天后收取,'.$getNotes['data']['hit_fee'].'元/年';
            $str6 = '以上费用扣取后不退还,如推销人员承诺其他事项';
            $str7 = ',与乐刷无关。';
            $str8 = '本人确认填写及提交的所有内容均为本人真实合法';
            $str9 = '有效的个人信息。';

            // $font = public_path('font')."/simhei.ttf";//加载字体ttf
            $font = realpath('font/simhei.ttf');//加载字体ttf(针对laravel框架本地运行无法直接引入时请使用此函数,将字体文件放在根目录(pulic目录)即可)
            $img = imagecreatefrompng($bgimg);// 加载背景图
            $color = imagecolorallocate($img, 0, 0, 0);//设置字体颜色
            //第一排加粗   x ,y 设置xy轴距离 str 填充字符串
            imagettftext($img, 15, 0, 135, 60, $color, $font, $str);
            imagettftext($img, 15, 0, 136, 60, $color, $font, $str);
            //第二排加粗
            imagettftext($img, 12, 0, 20, 120, $color, $font, $str2);
            imagettftext($img, 12, 0, 21, 120, $color, $font, $str2);

            //第三排
            imagettftext($img, 10, 0, 21, 160, $color, $font, $str3);
            //第四排
            imagettftext($img, 10, 0, 21, 178, $color, $font, $str4);
            if(!empty($getNotes['data']['hit_fee'])) {
                //第五排判断 hit_fee
                imagettftext($img, 10, 0, 21, 195, $color, $font, $str5);
            }
            //第六排
            imagettftext($img, 10, 0, 21, 210, $color, $font, $str6);
            //第七排
            imagettftext($img, 10, 0, 21, 225, $color, $font, $str7);
            //第八排
            imagettftext($img, 10, 0, 21, 500, $color, $font, $str8);
            //第九排
            imagettftext($img, 10, 0, 21, 520, $color, $font, $str9);
            $base64dir = '';
            //处理签名图片背景颜色以及缩放比列
            if($type=='agent'){ //展业端传图base64
                $base64dir =  storage_path('app') .'/public/'. mt_rand(99, 999) . time() . ".png";
                $src = base64_decode($contract) ; //获取签名图片临时地址
                file_put_contents($base64dir, $src);
                $src = $base64dir;
            }else{//商户传图
                $src = $contract->getRealPath(); //获取签名图片临时地址
            }
            $src_im = imagecreatefrompng($src); //创建图片
            $fx = imagesx($src_im); // 获取宽度
            $fy = imagesy($src_im); // 获取高度
            $small = imagecreatetruecolor(320,200); //签名图处理透明背景色为白色
            $color = imagecolorallocate($small,229,231,230); //上色
            imagecolortransparent($small,$color);//设置透明
            imagefill($small,0,0,$color);//填充
            imagecopyresampled($small,$src_im,0,0,0,0,320,200,$fx,$fy); //把签名图缩放为 $small
            imagecopy($img,$small,0, 260,0,0,320, 200); //合并背景图和$small 签名图

            //最终合成图保存地址
            $local_path = storage_path('app') .'/public/'. mt_rand(99, 999) . time() . ".png";
            //保存合成图片到本地
            $save_img = imagepng($img, $local_path);
            //销毁资源
            imagedestroy($img);
            imagedestroy($small);
            imagedestroy($src_im);

            if(empty($save_img))    return ['status'=>false, 'message'=> '图片操作失败!'];
            //上传阿里云
            $local = file_get_contents($local_path);
            $file_path = $data['path'] . '.png';
            $oss_temp = Storage::disk('oss-private')->put($file_path, $local);
            if (!$oss_temp){
                return ['status'=>false, 'message'=> '图片操作失败!'];
            }else {
                @unlink($local_path);//删除本地合成图返回oss图片地址
                if($type=='agent') @unlink($base64dir);
                return ['status'=>true, 'path'=> $file_path, 'message'=> '图片操作成功!'];
            }

说明 一张白色底图(320*640) + 合成文字 ,+ 前端传过来的签名图片 pNG格式 或者base64格式 进行缩放到相应大小 合成上去

posted @ 2022-07-28 18:39  Bashuslovakia  阅读(66)  评论(0编辑  收藏  举报