PHP如何生成二维码图片
在我们开发过程中,往往我们会遇到将某个商品或者将某个活动生成一个二维码,当用户扫描二维码时候,用户可以直接跳转到商品详细页面或者活动详情页面。那我们如何生成一个二维码图片呢,来,看这里哦!!!
1.封装
代码篇:
function QRcode Logo($url='',$logo='',$filepath,$level=3,$size=20){ Vendor('phpqrcode.phpqrcode');//引入PHP qrcode $Level =intval($level) ;//容错级别 $matrixPointSize = intval($size);//生成图片大小 //生成二维码图片 $object = new \QRcode(); $time = time(); $object:: png ($url, $filepath, $Level, $matrixPointSize, 2,true); $QR = $filepath; if ($logo !== FALSE ) { $QR = imagecreatefromstring(file_get_contents($QR)); $logo = imagecreatefromstring(file_get_contents($logo)); $QR_width = imagesx($QR);//二维码图片宽度 $QR_height = imagesy($QR);//二维码图片高度 $logo_width = imagesx($logo);//logo图片宽度 $logo_height = imagesy($logo);//logo图片高度 $logo_qr_width = $QR_width / 5; $scale = $logo_width/$logo_qr_width; $logo_qr_height = $logo_height/$scale; $from_width = ($QR_width - $logo_qr_width) / 2; //重新组合图片并调整大小 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } //输出图片 $path = '/qrcode/logo/' . time() . rand(100, 999) . '_qrcode.png'; $result = imagepng($QR, '.'.$path); return $path; }
2.看这里我们来调用使用啊
代码篇(片段):
foreach ($goods as $k => $v) { $url = ' . $v['goods_id'] . '&roomid=' . $v['apply_id'] . '&saletype=1'; $qrcode_path = "/qrcode/" . time() . rand(100, 999) . '_qrcode.png'; $path = qrcodeLogo($url, $v['goods_img'], './' . $qrcode_path, 8, 9); $goods[$k]['goods_code'] = ' . $path; if($v['tbUrl']!==''){ $url2 = $v['tbUrl']; $qrcode_path2 = "/qrcode/" . time() . rand(100, 999) . '_qrcode.png'; $path2 = qrcodeLogo($url2, $v['goods_img'], './' . $qrcode_path2, 8, 9); $goods[$k]['tao_goods_code'] = ' . $path2; }else{ $goods[$k]['tao_goods_code'] =''; } if($v['jdUrl']!==''){ $url3 = $v['jdUrl']; $qrcode_path3 = "/qrcode/" . time() . rand(100, 999) . '_qrcode.png'; $path3 = qrcodeLogo($url3, $v['goods_img'], './' . $qrcode_path3, 8, 9); $goods[$k]['dong_goods_code'] = ' . $path3; }else{ $goods[$k]['dong_goods_code'] =''; } if($v['jdUrl']!=='' || $v['tbUrl']!==''){ $url4 = ' address ='.$live['address'].'&shop_id='.$live['gl_merchants_id']; $qrcode_path4 = "/qrcode/" . time() . rand(100, 999) . '_qrcode.png'; $path4 = qrcodeLogo($url4, $v['goods_img'], './' . $qrcode_path4, 8, 9); $goods[$k]['ti_goods_code'] = ' . $path4; }else{ $goods[$k]['ti_goods_code'] =''; } }
海报
0 条评论
191
相关文章
本站已关闭游客评论,请登录或者注册后再评论吧~