$save_file = MY_ROOT_PATH."/uploads/qrcode_wxa/";//项目存储二维码的目录地址,自定义
if (!is_dir($save_file)){
mkdir($save_file);
}
$save_file .= 'shanghu_'.$suppliersn.".png";//自定义二维码名称,最好不重复
if (!file_exists($save_file)){
$value = 'http://www.baidu.com?sellerid='.$suppliersn;//扫描跳转的地址,参数自己拼接
$header_img = $head_logo_file;//二维码中间的头像,可忽略
$this->_abot_qrcode_with_header($value, $save_file, $header_img);
}
$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
//取二维码通过http打头的图片地址(刚刚自己定义的存储位置)
$qrcode_url = $sys_protocal.$this->get_current_domain().__ROOT__."/uploads/qrcode_wxa/".'shanghu_'.$suppliersn.".png";
//封装了生成二维码的方法
protected function _abot_qrcode_with_header($value, $qrcode_img, $header_img){
require_once APP_PATH.'Extend/Vendor/phpqrcode/phpqrcode.php';//生成二维码的类
$errorCorrectionLevel = 'H';//容错级别
$matrixPointSize = 10;//生成图片大小
//生成二维码图片
$ret = QRcode::png($value, $qrcode_img, $errorCorrectionLevel, $matrixPointSize, 2);
$logo = $header_img;//准备好的logo图片
$QR = $qrcode_img;//已经生成的原始二维码图
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);
}
//输出图片
imagepng($QR, $qrcode_img);
//echo '<img src="'.$qrcode_img.'">';
}