一、利用Google API生成二维码
Google提供了较为完善的二维码生成接口,调用API接口很简单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$urlToEncode="https://blog.wanxiaohong.cn"; generateQRcode($urlToEncode); /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param string $chl 二维码包含的信息,可以是数字、字符、二进制信息、汉字。 不能混合数据类型,数据必须经过UTF-8 URL-encoded * @param int $widhtHeight 生成二维码的尺寸设置 * @param string $EC_level 可选纠错级别,QR码支持四个等级纠错,用来恢复丢失的、读错的、模糊的、数据。 * L-默认:可以识别已损失的7%的数据 * M-可以识别已损失15%的数据 * Q-可以识别已损失25%的数据 * H-可以识别已损失30%的数据 * @param int $margin 生成的二维码离图片边框的距离 */ function generateQRcode($chl,$widhtHeight ='150',$EC_level='L',$margin='0') { $chl = urlencode($chl); echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'&choe=UTF-8" alt="QR code" widhtHeight="'.$widhtHeight.' " widhtHeight="'.$widhtHeight.'"/>'; } |
二、使用PHP二维码生成类库PHP QRcode生成二维码
PHP QRcode是一个PHP二维码生成类库,利用它可以轻松生成二维码。点击下载 && 点击查看官网
使用phpqrcode的必要条件PHP环境必须开启支持GD2。
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 |
require 'phpqrcode.php'; $value = 'https://blog.wanxiaohong.cn'; //二维码内容 $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = '10'; //生成图片大小 $margin = '0'; //生成的二维码离图片边框的距离 //生成二维码图片 QRcode::png($value, 'images/blog_qrcode.png', $errorCorrectionLevel, $matrixPointSize, $margin); $logo = 'phone.png'; //准备好的logo图片 $QR = 'images/blog_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, 'images/blog_qrcode.png'); //至此已生成带logo的二维码 echo '<img src="images/blog_qrcode.png">'; |
下面继续给一个可以直接调用的方法:
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 30 31 32 |
require 'phpqrcode.php'; $text = $_GET['text']; //二维码内容 $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = '10'; //生成图片大小 $margin = '0'; //生成的二维码离图片边框的距离 //生成二维码图片 QRcode::png($text, 'images/blog_qrcode.png', $errorCorrectionLevel, $matrixPointSize, $margin); $logo = 'phone.png'; //准备好的logo图片 $QR = 'images/blog_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); } //输出图片 Header("Content-type: image/png"); imagepng($QR); //访问方法 echo '<img src="https://blog.wanxiaohong.cn/tools/suvan_qrcode.php?text=https://blog.wanxiaohong.cn">'; |

2017年06月09日 00:16 沙发
万总66666666666666