二维码 ,又称二维条形码,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理。它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化等特点。
PHP生成QR Code的google API
$urlToEncode="http://www.google.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.'"/>';
}
PHP生成QR Code的 php QR Code 类库
<?php
require_once "phpqrcode.php";
class Custom_QRcode extends Custom_Object {
private $qrcodePath;
public function __construct($option=array()){
$_option=array(
"basePath"=>APPLICATION_PATH."/",
"relativePath"=>"/",
"errorCorrectionLevel"=>"L",//// 纠错级别:7%L、15%M、25%Q、30%H--字碼可被修正
"matrixPointSize"=>4,//100x100--5:125--6:150.....(max10)
"margin"=>2,
);
$this->setConfig($_option);
$this->setConfig($option);
}
/*
$data 数据
$filename 保存的图片名称
$errorCorrectionLevel 错误处理级别
$matrixPointSize 每个黑点的像素
$margin 图片外围的白色边框像素
*/
public function createQRcode($data=''){
if(!$data){return false;}
$options = $this->_option;
$PNG_TEMP_DIR = $options['basePath'].$options['relativePath'].'/';
if(!is_dir($PNG_TEMP_DIR)){
mkdir($PNG_TEMP_DIR,0777,true);//php 5
}
$errorCorrectionLevel = $options['errorCorrectionLevel'];
$matrixPointSize = $options['matrixPointSize'];
$margin = $options['margin'];
$qrname = $this->getNewFileName($data);
$this->qrcodePath = $options['relativePath'].'/'.$qrname;
$filename = $PNG_TEMP_DIR.$qrname;
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, $margin);
return true;
}
/*** 设置配置选项* * @param $option* @return void*/
public function setConfig($option){
foreach($option as $key=>$val){
$this->_option[$key]=$val;
}
}
/*獲取文件名*/
public function getNewFileName($data){
$ecl = $this->_option['errorCorrectionLevel'];
$mps = $this->_option['matrixPointSize'];
$md5 = md5($data.'|'.$ecl.'|'.$mps);
$extName = '.png';
return substr($md5,8,16).rand(1000,10000).$extName;
}
//獲取圖片相對路徑;
public function getQrPicPath(){
return $this->qrcodePath;
}
}
//生成二维码用法
$qrdata = 'http://sourceforge.net/projects/phpqrcode/';//qrcode 类库下载地址
$QRcode = new Custom_QRcode(array(
"relativePath"=>"/qrcode/",
));
$QRcode->createQRcode($qrdata);
$qrcodesrc = $QRcode->getQrPicPath(); JQuery生成QR Code
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>
<div id="qrcode"></div>
<script type="text/javascript">
$("#qrcode").qrcode("helin");
//或者你可以设置生成的二维码的宽和高还有颜色等
$("#qrcode").qrcode({
render: 'canvas',// render method: 'canvas' or 'div'
// width and height in pixel
width: 100,
height: 100,
color: '#3a3',// QR code color
bgColor: null,// background color, null for transparent background
text: 'http://qrcode'// the encoded text
});
</script>
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com