您的位置:首页 > 编程语言 > PHP开发

简单的 自动生成 二维码 PHP 方法

2016-03-31 12:12 591 查看
方法一:
<style type="text/css">
.eweima{
width:200px; height:200px; margin:auto;
}
</style>
<div class="eweima">
<img src="http://localhost/xianhewang/index.php?a=user&user_id=82 width="200" height="200" alt="二维码"/>

</div>

上面是所有代码,创建一个小工具把代码复制到里面就好了,代码中的 200 为二维码的高宽,高宽一定要一样,你可以根据自己的边栏宽度更改下。这套代码是利用了 google 的 API 实现的,所以速度上肯定是占优势的了。

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

方法二:
一般网页中也可使用下面的代码:我用html测试OK。

<script>
thisURL = document.URL;
strwrite = "<img src='https://chart.googleapis.com/chart?cht=qr&chs=150x150&choe=UTF-8&chld=L|4&chl=" + thisURL + "' width='150' height='150' alt='QR 码' />";
document.write( strwrite );
</script>

这个用的PHP代码中也可以使用下面的代码:

<img src=" the_permalink();?>" width="150" height="150" alt="QR 码" />

希望对大家能用到。

——————————————————————————————————————————
织梦系统可直接用下面带边框的:

<!--二维码生成 -->
<div class="zly1"><div class="zlx1">
<dl class="t11 l11">
<dt class='light'><strong>本页二维码</strong></dt>
<dd class='light'>
<ul class="nb1">
<style type="text/css">
.eweima{
width:200px;
height:200px;
margin:auto;

}
</style>
<div class="eweima">
<script type="text/javascript">
document.write("<img src=\"https://chart.googleapis.com/chart?cht=qr&chs=500x500&choe=UTF-8&chld=L|2&chl=");
document.write(window.location.href);
document.write("\" width=\"200\" height=\"200\" alt=\"二维码\"/>");
</script>
</ul></dd>
</dl>
</div> </div>
<!--二维码生成//-->

php 方法
一、利用Google API生成二维码
Google提供了较为完善的二维码生成接口,调用API接口很简单,以下是调用代码:

. 代码如下:

$urlToEncode="http://www.jb51.net";
generateQRfromGoogle($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 generateQRfromGoogle($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.'" alt="QR code" widhtHeight="'.$widhtHeight.'
" widhtHeight="'.$widhtHeight.'"/>';
}

二、使用PHP二维码生成类库PHP QR Code生成二维码

PHP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo,查看地址:http://phpqrcode.sourceforge.net/。
下 载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2。 phpqrcode.php提供了一个关键的png()方法,其中参数$text表示生成二位的的信息文本;参数$outfile表示是否输出二维码图片 文件,默认否;参数$level表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%); 参数$size表示生成图片大小,默认是3;参数$margin表示二维码周围边框空白区域间距值;参数$saveandprint表示是否保存二维码并 显示。

. 代码如下:

public static function png($text, $outfile=false, $level=QR_ECLEVEL_L, $size=3, $margin=4,
$saveandprint=false)
{
$enc = QRencode::factory($level, $size, $margin);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
}

调用PHP QR Code非常简单,如下代码即可生成一张内容为"http://www.jb51.net"的二维码.
Php代码
include 'phpqrcode.php';
QRcode::png('http://www.jb51.net');

那 么实际应用中,我们会在二维码的中间加上自己的LOGO,已增强宣传效果。那如何生成含有logo的二维码呢?其实原理很简单,先使用PHP QR Code生成一张二维码图片,然后再利用php的image相关函数,将事先准备好的logo图片加入到刚生成的原始二维码图片中间,然后重新生成一张新 的二维码图片。

. 代码如下:

include 'phpqrcode.php';
$value = 'http://www.jb51.net'; //二维码内容
$errorCorrectionLevel = 'L';//容错级别
$matrixPointSize = 6;//生成图片大小
//生成二维码图片
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo.png';//准备好的logo图片
$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, 'helloweba.png');
echo '<img src="helloweba.png">';

由于二维码允许有一定的容错性,一般的二维码即使在遮住部分但仍然能够解码,经常我们扫描二维码的时候扫描到甚至不到一半时就能解码扫描结果,这是因为生成器会将部分信息重复表示来提高其容错度,这就是为什么我们在二维码中间加个LOGO图片并不影响解码结果的原因。

网页中嵌入效果:

<!DOCTYPE html>
<html>
<head>
<title>basic example</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<!--<script type="text/javascript" src="../jquery.qrcode.min.js"></script>
--><script type="text/javascript" src="../src/jquery.qrcode.js"></script>
<script type="text/javascript" src="../src/qrcode.js"></script>
<p>Render in table</p>
<div id="qrcodeTable"></div>
<p>Render in canvas</p>
<div id="qrcodeCanvas"></div>
<script>
//jQuery('#qrcode').qrcode("this plugin is great");
jQuery('#qrcodeTable').qrcode({
render : "table",
text : "http://jetienne.com"
});
jQuery('#qrcodeCanvas').qrcode({
text : "http://jetienne.com"
});
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: