米
心情
所有
图集
登录
搜索
原创
php利用endroid/qr-code+Intervention Image生成带二维码加文字的海报实例
米醋儿
发布于:2020-03-28
今天要做一个分享海报,难度不大,对于新手还是很有帮助的 [](https://micuu.com/data/upload/avatar/20200328/ec395894446e02dce89c5f84c6cded75.jpg) 二维码生成代码 ``` public function make_qrcode($res,$params) // public function make_qrcode() { // $res = Db::name("users_celebrity")->find(72); // // $params = [ // 'name'=>"zhanwan", // 'cid'=>"70", // 'uid'=>"75", // ]; $path = "./application/".date("Ymd"); if(!is_array($params)){ die('params不是数组形式'); } $url =request()->domain().url('index/detail/index')."?id={$res['u_id']}&"; foreach ($params as $k => $v){ $url .= $k.'='.$v."&"; } // Create a basic QR code $qrCode = new QrCode($url); $qrCode->setSize(120); // Set advanced options $qrCode->setWriterByName('png'); $qrCode->setMargin(0); $qrCode->setEncoding('UTF-8'); $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH()); $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]); $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]); //$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER()); // $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png'); //$qrCode->setLogoSize(150, 200); $qrCode->setRoundBlockSize(true); $qrCode->setValidateResult(false); $qrCode->setWriterOptions(['exclude_xml_declaration' => true]); // Directly output the QR code header('Content-Type: '.$qrCode->getContentType()); $qrCode->writeString(); // Save it to a file $qrCode->writeFile("{$path}/ewm_{$res['u_id']}.jpg"); // Create a response object $response = new QrCodeResponse($qrCode); return $response; } ``` 图片处理 单人 ``` //单人类型 海报生成 public function type_one($res,$path,$params=[]) { // 通过指定 driver 来创建一个 image manager 实例 $manager = new ImageManager(array('driver' => 'imagick')); if(!is_file("{$path}/head_{$res['u_id']}.jpg")){ $head = $res["u_headimgurl"]; $i = $manager->make($head); $i->resize(102,137); $i->save("{$path}/head_{$res['u_id']}.jpg"); } //二维码生成 参数不一样 二维码每次需要动态生成 $this->make_qrcode($res,$params); // if(!is_file("./application/ewm_{$res['u_id']}.jpg")){ // $this->make_qrcode($res); // } // 最后创建 image 实例 - 单人堂海报创建 $image = $manager->make('./jizu/poster/one.jpg'); //头像坐标开始 190 915 $image->insert("{$path}/head_{$res['u_id']}.jpg", 'top-left', 170, 915); //二维码坐标 643 922 px $image->insert("{$path}/ewm_{$res['u_id']}.jpg", 'top-left', 613, 910); //姓名文字 开始坐标 308 919 //$image->insert('./jizu/ewm.jpg'); //$name = $this->to_unicode($res['u_name']); $name = $res['u_name']; //p($name);die; $image->text($name, 288, 919, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(24); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //祭奠值 8888 坐标开始位置 377 963 $image->text($res["u_value"], 377, 963, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(16); $font->color('#d3a554'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //生辰坐标开始位置362 995 $image->text($res["u_birthday"], 362, 995, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(16); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //逝世坐标开始位置362 1027 $image->text($res["u_away_time"], 362, 1027, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(16); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); $image->save("{$path}/poster{$res['u_id']}.jpg"); $img_url = request()->domain()."/application/".date("Ymd")."/poster{$res['u_id']}.jpg"; return $img_url; } ``` 图片处理双人 ``` //双人类型 海报生成 public function type_two($res,$path,$params=[]) { // 通过指定 driver 来创建一个 image manager 实例 $manager = new ImageManager(array('driver' => 'imagick')); //第一个人头像 if(!is_file("{$path}/head_{$res['u_id']}.jpg")){ $head = $res["u_headimgurl"]; $i = $manager->make($head); $i->resize(102,137); $i->save("{$path}/head_{$res['u_id']}.jpg"); } //第二个人头像 if(!is_file("{$path}/head_{$res['user_two']['t_id']}.jpg")){ $head = $res['user_two']['t_headimgurl']; $i = $manager->make($head); $i->resize(102,137); $i->save("{$path}/head_{$res['user_two']['t_id']}.jpg"); } //二维码生成 参数不一样 二维码每次需要动态生成 $this->make_qrcode($res,$params); // if(!is_file("./application/ewm_{$res['u_id']}.jpg")){ // $this->make_qrcode($res); // } // 最后创建 image 实例 - 单人堂海报创建 $image = $manager->make('./jizu/poster/two.jpg'); //头像 1 坐标开始 83 915 $image->insert("{$path}/head_{$res['u_id']}.jpg", 'top-left', 83, 915); //头像 2 坐标开始 195 915 $image->insert( "{$path}/head_{$res['user_two']['t_id']}.jpg", 'top-left', 195, 915); //二维码坐标 643 922 px $image->insert("{$path}/ewm_{$res['u_id']}.jpg", 'top-left', 695, 912); //姓名1 文字 开始坐标 315 915 $name = $res['u_name']; $image->text($name, 315, 915, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(24); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //姓名2 文字 开始坐标 315 975 $name = $res['user_two']['t_name']; $image->text($name, 315, 975, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(24); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //人员1 逝世 等信息 315 943 $text1 = "生辰:{$res["u_birthday"]}逝世:{$res["u_away_time"]}"; $image->text($text1, 315, 943, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(16); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //人员2 逝世 等信息 315 1005 u_away_time $text2 = "生辰:{$res['user_two']["t_birthday"]}逝世:{$res['user_two']["t_away_time"]}"; $image->text($text2, 315, 1005, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(16); $font->color('#999999'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); //祭奠值 8888 坐标开始位置 315 1035 $image->text("祭奠值:".$res["u_value"], 315, 1035, function($font) { $font->file('./jizu/msyh.ttf'); $font->size(16); $font->color('#d3a554'); $font->align('left'); $font->valign('top'); //$font->angle(45); }); $image->save("{$path}/poster{$res['u_id']}.jpg"); $img_url = request()->domain()."/application/".date("Ymd")."/poster{$res['u_id']}.jpg"; return $img_url; } ``` 主体函数 ``` public function poster1() { $id = request()->param("id"); $params = request()->param("params"); // finally we save the image as a new file $path = "./application/".date("Ymd"); if (!is_dir($path)){ mkdir($path,0777); } $res =\app\common\model\UsersCelebrity::with("user_two")->find($id); // p($res);die; if($res['u_type'] == 2){ $t = $this->type_two($res,$path,$params); // 双人堂类型 }else{ $t = $this->type_one($res,$path,$params); // 单人堂类型 } } ``` use ``` namespace app\index\controller; use app\common\model\Comment; use app\common\model\Favorite; use app\common\model\Users; use app\myclass\Curl; use EasyWeChat\Factory; use Firebase\JWT\JWT; use think\Db; use think\facade\Session; // 导入 Intervention Image Manager Class use Intervention\Image\ImageManager; // import the Intervention Image Manager Class use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\LabelAlignment; use Endroid\QrCode\QrCode; use Endroid\QrCode\Response\QrCodeResponse; use app\common\common\Token; use think\facade\Cache; ``` 以上缺少的包大家请用composer安装之后再测本程序,本程序是基于tp5.1的,有不足之处大家自行优化,字体我是用的微软雅黑,大家复制后替换自己服务器上的字体文件
注:原创不易,转载请注明出处(
https://micuu.com/new/475.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-02-21
上一篇:
开发常用一些开发及服务器软件
下一篇:
序列图片实现视频播放效果实例代码演示
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>