新建一个类文件code.php
load()->library('qrcode'); class code{ //生成二维码 //参数: $content:二维码的内容 string // $size:二维码的尺寸 string public function createCode($content,$size){ $errorCorrectionLevel = "L";//错误校正级别 $code = QRcode::png($content, false, $errorCorrectionLevel,$size); return $code; } //下载二维码 //参数:二维码的内容数据content 大小size public function downloadCode($content,$size){ $code = $this->createCode($content,$size); $name = random(8); header('cache-control:private'); header('content-type:image/jpeg'); header('content-disposition: attachment;filename="'.$name.'.jpg"'); readfile($code); } }
在需要用到此方法的地方实例化。(注意文件的路径问题,避免实例化出错)
$code = new code(); $res_code = $code ->createCode("这里放的是二维码的内容","5");//生成二维码 $download_code = $code ->downloadCode("这里是二维码的内容","5");//下载二维码