php 生成二维码
首先下载phpqrcode 库
下面是我在tp框架中写的Model
public function qrcode($url, $size = 8) { //定义文件存储路径 $file = 'Public/UploadFiles/qrcode/'.date('Y/m/d/', NOW_TIME); //phpqrcode无法自己创建文件夹所以需自己手动创建,这里我是用PHP函数来创建的 if(! is_dir($file)){ mkdir($file, 0777, true); } $filed = $file.'qrcode'.uniqid().'.png';
//引入phpqrcode require_cache(LIB_PATH . 'Vendor/phpqrcode/phpqrcode.php'); \QRcode::png($url, $filed, 'L', $size, 2,true); return $filed; }
//这是我调用接口的方法
//生成二维码 public function qrcode() { $custom = M('Custom')->where(['id' => $this->custom_id])->find(); $code = $custom['qrcode']; //为避免验证码出现太多所以我们将验证码文件路径存入数据库 if(! $custom['qrcode']){ $url = "http://www.youpingou.com/Mobile/Commission/giveCommission/customid/4082"; $code = D('Weixin')->qrcode($url); M('Custom')->where(['id' => $custom['id']])->save(['qrcode' => $code]); } wechatjson(1 , ['code' => $code]); }