一. 生成二维码
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);
}
function getbrtext($width = 200, $size = 16, $angle = 0, $font = '', $text = '') {
//判断是否超出范围
$str = "";
for ($i = 0; $i < mb_strlen($text); $i++) {
$letter[] = mb_substr($text, $i, 1);
}
foreach ($letter as $l) {
$teststr = $str . " " . $l;
$testbox = imagettfbbox($size, $angle, $font, $teststr);
// 判断拼接后的字符串是否超过预设的宽度。超出宽度添加换行
if (($testbox[2] > $width) && ($str !== "")) {
$str .= "\n";
}
$str .= $l;
}
//返回
return $str;
}
生成海报图: