首先我们使用composer下载此包
https://packagist.org/packages/aferrandini/phpqrcode
使用命名空间载入方法,最后使用 imagecopyresampled 函数重新绘制二维码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Home\Controller; use Think\Controller; use PHPQRCode; class IndexController extends Controller { public function index(){ require 'vendor\autoload.php'; \PHPQRCode\QRcode::png("Test", "qrcode.png", 'L', 4, 2); $logo = 'logo_top_ca79a146.png'; $QR = 'qrcode.png'; 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,'test.png'); }else{ ajaxReturnData(-1,'生成失败'); } } } |