首页 » 资源分享 » PHP » 正文

PHP使用phpqrcode生成二维码并强制下载



在之前的文章中,我们有讲到PHP怎么使用phpqrcode生成二维码,和PHP怎么使用phpqrcode生成带LOGO或文章的二维码,今天我们来讲另外一种情况,就是php怎么使用qrcode生成二维码并强制下载。这样有利于我们直接显示成图片易于保存,或直接下载二维码。
代码:
include 'phpqrcode.php';
$url = 'http://www.caizhichao.cn'; //二维码内容
$errorCorrectionLevel = 'H'; //容错级别
$matrixPointSize = 5; //生成图片大小
ob_start();
$QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize, 1);
$ob_contents = ob_get_contents(); //读取缓存区数据
ob_end_clean();
$myImage = ImageCreate(245,245); //参数为宽度和高度
$isShow = true ; //true为显示二维码,false为强制下载
if($isShow){ //如果想显示出来
Header("Content-type: image/png");
}else{ //如果直接下载
Header('Pragma: public'); // required
Header('Expires: 0'); // no cache
Header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
Header('Cache-Control: private',false);
Header('Content-Type: application/force-download');
Header('Content-Disposition: attachment; filename="krb_qrcode.png"');
Header('Content-Transfer-Encoding: binary');
Header('Connection: close');
}
imagepng($myImage);