php生成二维码的几种方式[转]

 

二维码是二维条形码的一种,可以将网址、文字、照片等信息通过相应的编码算法编译成为一个方块形条码图案,手机用户可以通过摄像头和解码软件将相关信息重新解码并查看内容。PHP可以使用php QR Code类库生成二维码。

二维码的应用范围可以点这里(百度百科)。
1.google开放api
$urlToEncode="http://gz.altmi.com";
generateQRfromGoogle($urlToEncode);
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
{
    $url = urlencode($url); 
    echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$size.'" widhtHeight="'.$size.'"/>';
}
2.php类库PHP QR Code
PHP QR Code is open source (LGPL) library for generating QR Code,
2-dimensional barcode. Based on libqrencode C library,
provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2).
Implemented purely in PHP, with no external dependencies (except GD2 if needed).

使用用例:
<?php
   include('./phpqrcode/phpqrcode.php');
   // 二维码数据
   $data = 'http://gz.altmi.com';
   // 生成的文件名
   $filename = $errorCorrectionLevel.'|'.$matrixPointSize.'.png';
   // 纠错级别:L、M、Q、H
   $errorCorrectionLevel = 'L'; 
   // 点的大小:1到10
   $matrixPointSize = 4; 
   QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
官方给出的用例:
<?php
# include这两个文件之一:
/*
qrlib.php for full version (also you have to provide all library files 
form package plus cache dir)

OR phpqrcode.php for merged version (only one file, 
but slower and less accurate code because disabled cache
and quicker masking configured)
*/
# 两句话解释:
# 包含qrlib.php的话需要同其它文件放到一起:文件、文件夹。
# phpqrcode.php是合并后版本,只需要包含这个文件,但生成的图片速度慢而且不太准确
# 以下给出两种用法:

# 创建一个二维码文件
QRcode::png('code data text', 'filename.png');
// creates file

# 生成图片到浏览器
QRcode::png('some othertext 1234');
// creates code image and outputs it directly into browser

地址:http://phpqrcode.sourceforge.net/
下载:http://sourceforge.net/projects/phpqrcode/

3.libqrencode

地址:http://fukuchi.org/works/qrencode/index.en.html
php支持请参考:http://hirokawa.netflowers.jp/entry/4900/

4.QRcode Perl CGI & PHP scripts
地址:http://www.swetake.com/qr/qr_cgi.html

原文:http://blog.chinaunix.net/uid-20787846-id-3492930.html

---------------

使用PHP QR Code类库创建二维码

使用举例浏览器输出:

<?
include "phpqrcode/phpqrcode.php";
$value="http://s.bookphone.cn/chinabook/index.php/adminhtml/Croles/admin";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>

文件输出二维码

include('phpqrcode/phpqrcode.php');
// 二维码数据
$data = 'http://s.bookphone.cn';
// 生成的文件名
$filename = '1111.png';
// 纠错级别:L、M、Q、H
$errorCorrectionLevel = 'L';
// 点的大小:1到10
$matrixPointSize = 4;
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

生成中间带logo的二维码

<?php
include('phpqrcode/phpqrcode.php');
$value='http://xy.bookphone.cn';
$errorCorrectionLevel = 'L';
$matrixPointSize = 6;
QRcode::png($value, 'xiangyang.png', $errorCorrectionLevel, $matrixPointSize, 2);
echo "QR code generated"."<br />";
$logo = 'logo.png';
$QR = 'xiangyang.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_height = imagesy($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,'xiangyanglog.png');
?>

原文地址:https://www.cnblogs.com/wellsoho/p/4424984.html