tp5.0 生成二維碼 + 合併海報圖
阿新 • • 發佈:2021-10-29
一. 生成二維碼
1.下載phpqrcode類 phpqrcode類庫官網下載地址:https://sourceforge.net/projects/phpqrcode/
2.在tp下extend建立資料夾ewm ,類庫下的phpqrcode.php放進去,可以改名為QRcode.php 並設定名稱空間。
3. 引用類庫 use ewm\QRcode;
public function qrcodes($ids){ //生成二維碼圖片 $url =""; //掃描二維跳轉連線 $path = "./public/qrcode/".$ids.".jpg"; //生成二維碼路徑 if(!file_exists('./public/qrcode/')){ mkdir('./public/qrcode/',0777,true); } $level=3; $size=5; $errorCorrectionLevel =intval($level); $matrixPointSize = intval($size); # 調取類庫 Extend (use ewm\QRcode;) QRcode檔案設定名稱空間 namespace ewm (ewm資料夾); $packet = new QRcode(); $packet->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2); //生成二維碼 $r_path = "./public/qrcode/".$ids.".jpg"; $img = $r_path; //返回路徑 return $img; }
4.二維碼+海報圖合併
public function user_qrcode(){ $id = $this->request->param('id', 999999); $path = "./public/qrcode/share/"; //二維碼和背景圖合併路徑 if (!is_dir($path)) { mkdir($path,0777,true); } $file2 = $path . time() .'_'.$id. '.png'; $QR = $this->qrcodes($id);//已經生成的原始二維碼圖 路徑 # 獲取會議 $meet = Db::name("meet")->where(["id"=>$id])->find(); # 會議類別 $meetcate = Db::name("meettype")->where(["id"=>$meet['producttype']])->value("name"); $config = array( 'text'=>array( array( 'text'=>$this->getbrtext(640,32,0,'./public/qrcode/simhei.ttf',$meet['title']), 'left'=>50, 'top'=>290, 'fontPath'=>'./public/qrcode/simhei.ttf', //字型檔案 'fontSize'=>32, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, //轉換角度 ), array( 'text'=>$meetcate, 'left'=>280, 'top'=>570, 'fontPath'=>'./public/qrcode/simhei.ttf', //字型檔案 'fontSize'=>28, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, ), array( 'text'=>'開始時間:'.$meet['starttime'], 'left'=>150, 'top'=>670, 'fontPath'=>'./public/qrcode/simhei.ttf', //字型檔案 'fontSize'=>22, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, ), array( 'text'=>'結束時間:'.$meet['endtime'], 'left'=>150, 'top'=>728, 'fontPath'=>'./public/qrcode/simhei.ttf', //字型檔案 'fontSize'=>22, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, ), array( 'text'=>'參會人數:'.$meet['zhifufangshi'], 'left'=>150, 'top'=>786, 'fontPath'=>'./public/qrcode/simhei.ttf', //字型檔案 'fontSize'=>22, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, ), array( 'text'=>$this->getbrtext(550,22,0,'./public/qrcode/simhei.ttf','會議地點:'.$meet['address']), 'left'=>150, 'top'=>849, 'fontPath'=>'./public/qrcode/simhei.ttf', //字型檔案 'fontSize'=>22, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, ), ), 'image'=>array( array( 'url'=>$QR, //二維碼資源 'stream'=>0, 'left'=>260, //二維碼位置 'top'=>-160, 'right'=>0, 'bottom'=>0, 'width'=>178, 'height'=>178, 'opacity'=>100 ) ), ); //如果要看報什麼錯,可以先註釋調這個 header $imageDefault = array( 'left'=>0, 'top'=>0, 'right'=>0, 'bottom'=>0, 'width'=>100, 'height'=>100, 'opacity'=>100 ); $textDefault = array( 'text'=>'', 'left'=>0, 'top'=>0, 'fontSize'=>32, //字號 'fontColor'=>'255,255,255', //字型顏色 'angle'=>0, ); $background = './public/qrcode/imgs/51.jpg';//海報最底層得背景 //背景方法 $backgroundInfo = getimagesize($background); $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false); $background = $backgroundFun($background); $backgroundWidth = imagesx($background); //背景寬度 $backgroundHeight = imagesy($background); //背景高度 $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight); $color = imagecolorallocate($imageRes, 0, 0, 0); imagefill($imageRes, 0, 0, $color); // imageColorTransparent($imageRes, $color); //顏色透明 imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background)); //處理了圖片 if(!empty($config['image'])){ foreach ($config['image'] as $key => $val) { $val = array_merge($imageDefault,$val); $info = getimagesize($val['url']); $function = 'imagecreatefrom'.image_type_to_extension($info[2], false); if($val['stream']){ //如果傳的是字串影象流 $info = getimagesizefromstring($val['url']); $function = 'imagecreatefromstring'; } $res = $function($val['url']); $resWidth = $info[0]; $resHeight = $info[1]; //建立畫板 ,縮放圖片至指定尺寸 $canvas=imagecreatetruecolor($val['width'], $val['height']); imagefill($canvas, 0, 0, $color); //關鍵函式,引數(目標資源,源,目標資源的開始座標x,y, 源資源的開始座標x,y,目標資源的寬高w,h,源資源的寬高w,h) imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight); $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left']; $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top']; //放置影象 imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,寬度,高度,透明度 } } # 處理文字 if(!empty($config['text'])){ foreach ($config['text'] as $key => $val) { $val = array_merge($textDefault,$val); list($R,$G,$B) = explode(',', $val['fontColor']); $fontColor = imagecolorallocate($imageRes, $R, $G, $B); $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left']; $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top']; imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']); } } $res = imagejpeg ($imageRes,$file2,90); //儲存到本地 imagedestroy($imageRes); # 刪除之前圖片 $del_meet_info = Db::name('meet')->where(['id'=>$id])->find(); if ($del_meet_info['poster']) { # 獲取並刪除... $filename = $del_meet_info['poster']; if (file_exists($filename)) { unlink($filename); } } $res = Db::name('meet')->where(['id'=>$id])->update(["poster" => $file2]); if ($res) { $meet = Db::name('meet')->where(['id'=>$id])->find(); $poster = substr($meet['poster'],1); if ($meet) { $result = array("code" => 1, "msg" => "生成二維碼成功!","pic" => $poster); }else{ $result = array("code" => 1, "msg" => "暫無圖片"); } }else{ $result = array("code" => 0, "msg" => "生成二維碼失敗!"); } return json($result); }
生成海報圖: