thinkphp5生成二维码,带logo,支持添加文字
php相关 /
2019年12月27日 14时39分 /
6280人浏览
1.首先我们先下载一下phpqrcode类库,需要用到phpqrcode类库.
2.取出phpqrcode文件夹,然后放到extend文件夹里面.
3.在控制器里面写入并调用以下方法,即可看到你要的二维码了.
public function qr() {
$house = Db::name('house')->where('id', input('id'))->find();
$info = url('member/house/index', ['id' => $house['id']], true, true);
$qr_name = 'house_' . $house['id'] . '.png';
$pic = DIR_IMAGE . 'images/qrcode/' . $qr_name;
import('phpqrcode.phpqrcode', EXTEND_PATH, '.php');
$errorCorrectionLevel = "H";
$matrixPointSize = "10";
\QRcode::png($info, $pic, $errorCorrectionLevel, $matrixPointSize);
$logo = ROOT_PATH . 'public/static/image/sbyp2.png'; //准备好的logo图片
$QR = $pic; //已经生成的原始二维码图
if (file_exists($logo)) {
//字体大小
$size = 45;
//字体类型,本例为黑体
$font = ROOT_PATH . 'public/static/assets/fonts/simsun.ttc';
//显示的文字
$text = $house['name'];
$logo = imagecreatefromstring(file_get_contents($logo)); // 加载已有图像
//设置字体颜色
$black = ImageColorAllocate($logo, 0, 0, 0);
//将ttf文字写到图片中
imagettftext($logo, $size, 0, 100, 385, $black, $font, $text);
imagettftext($logo, $size, 0, 101, 385, $black, $font, $text);
$QR = imagecreatefromstring(file_get_contents($QR)); //目标图象连接资源。
$QR_width = imagesx($QR); //二维码图片宽度
$QR_height = imagesy($QR); //二维码图片高度
$logo_width = imagesx($logo); //logo图片宽度
$logo_height = imagesy($logo); //logo图片高度
$logo_qr_width = $QR_width / 4; //组合之后logo的宽度(占二维码的1/5)
$scale = $logo_width / $logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height / $scale; //组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
//重新组合图片并调整大小
/*
* imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
*/
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
//输出图片
imagepng($QR, $pic);
imagedestroy($QR);
imagedestroy($logo);
$this->assign('qr', $qr_name);
$this->assign('house', $house);
$this->assign('breadcrumb2', '大棚信息');
return $this->fetch('qr');
}