网站建设

php自动生成二维码

PHP生成二维码的两种方法,1.使用google的api生成,2.使用PHP QR Code生成。

一、PHP使用google api在线生成二维码:

关于这个API的介绍就不说了吧,你可以去Google的官方网站查询,相信大家关注的是具体代码,如下:

<?php
$urlToEncode=”http://www.csdn.net”;//要生成二维码的网址
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.'”/>’;//Google API接口,若失效可到Google网址查询最新接口
}
?>

<?php
include “phpqrcode.php”;//引入PHP QR库文件
$value=”http://www.csdn.net”;
$errorCorrectionLevel = “L”;
$matrixPointSize = “4”;
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>

PHP QR Code是一个开源的php二维码开源类库,基于libqrencode C库,并提供API代码创建QR条码图像,支持png、jpg格式。若没有这个库,可在此下载:PHP QR Code二维码生成库

浏览器输出

<?
include "phpqrcode/phpqrcode.php";
$errorCorrectionLevel "L";
$matrixPointSize "4";
QRcode::png($value, false, $errorCorrectionLevel$matrixPointSize);
exit;
?>

文件输出

include('phpqrcode/phpqrcode.php');
// 二维码数据
$data 'http://www.jb51.net';
// 生成的文件名
$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');
$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');
?>

Related Articles

发表回复

Back to top button