php生成二維碼
隨著科技的進(jìn)步,大家經(jīng)常見到條形碼和二維碼,特別是智能手機(jī)時(shí)代這些碼更顯得活躍了,今天我給大家分享下如何使用PHP生成二維碼,至于如何生成二維碼,一般常用的試調(diào)用google的開放api來生成,言歸正傳,直接上代碼了:
第一種方法:
利用Google API生成二維碼
Google提供了較為完善的二維碼生成接口,調(diào)用API接口很簡(jiǎn)單,以下是調(diào)用代碼:
$urlToEncode="http://www.dbjr.com.cn"; generateQRfromGoogle($urlToEncode); /** * google api 二維碼生成【QRcode可以存儲(chǔ)最多4296個(gè)字母數(shù)字類型的任意文本,具體可以查看二維碼數(shù)據(jù)格式】 * @param string $chl 二維碼包含的信息,可以是數(shù)字、字符、二進(jìn)制信息、漢字。 不能混合數(shù)據(jù)類型,數(shù)據(jù)必須經(jīng)過UTF-8 URL-encoded * @param int $widhtHeight 生成二維碼的尺寸設(shè)置 * @param string $EC_level 可選糾錯(cuò)級(jí)別,QR碼支持四個(gè)等級(jí)糾錯(cuò),用來恢復(fù)丟失的、讀錯(cuò)的、模糊的、數(shù)據(jù)。 * L-默認(rèn):可以識(shí)別已損失的7%的數(shù)據(jù) * M-可以識(shí)別已損失15%的數(shù)據(jù) * Q-可以識(shí)別已損失25%的數(shù)據(jù) * H-可以識(shí)別已損失30%的數(shù)據(jù) * @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是一個(gè)PHP二維碼生成類庫,利用它可以輕松生成二維碼,官網(wǎng)提供了下載和多個(gè)演示demo,查看地址:http://phpqrcode.sourceforge.net/。
下載官網(wǎng)提供的類庫后,只需要使用phpqrcode.php就可以生成二維碼了,當(dāng)然您的PHP環(huán)境必須開啟支持GD2。phpqrcode.php提供了一個(gè)關(guān)鍵的png()方法,其中參數(shù)$text表示生成二位的的信息文本;參數(shù)$outfile表示是否輸出二維碼圖片文件,默認(rèn)否;參數(shù)$level表示容錯(cuò)率,也就是有被覆蓋的區(qū)域還能識(shí)別,分別是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%);參數(shù)$size表示生成圖片大小,默認(rèn)是3;參數(shù)$margin表示二維碼周圍邊框空白區(qū)域間距值;參數(shù)$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); }
調(diào)用PHP QR Code非常簡(jiǎn)單,如下代碼即可生成一張內(nèi)容為"http://www.dbjr.com.cn"的二維碼.
include 'phpqrcode.php'; QRcode::png('http://www.dbjr.com.cn');
那么實(shí)際應(yīng)用中,我們會(huì)在二維碼的中間加上自己的LOGO,已增強(qiáng)宣傳效果。那如何生成含有l(wèi)ogo的二維碼呢?其實(shí)原理很簡(jiǎn)單,先使用PHP QR Code生成一張二維碼圖片,然后再利用php的image相關(guān)函數(shù),將事先準(zhǔn)備好的logo圖片加入到剛生成的原始二維碼圖片中間,然后重新生成一張新的二維碼圖片。
include 'phpqrcode.php'; $value = 'http://www.dbjr.com.cn'; //二維碼內(nèi)容 $errorCorrectionLevel = 'L';//容錯(cuò)級(jí)別 $matrixPointSize = 6;//生成圖片大小 //生成二維碼圖片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'logo.png';//準(zhǔn)備好的logo圖片 $QR = 'qrcode.png';//已經(jīng)生成的原始二維碼圖 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; //重新組合圖片并調(diào)整大小 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } //輸出圖片 imagepng($QR, 'jb51.png'); echo '<img src="jb51.png">';
方法二:
不廢話了,直接上代碼了:
<?php function UPCAbarcode($code) { $lw = 2; $hi = 100; $Lencode = array('0001101','0011001','0010011','0111101','0100011', '0110001','0101111','0111011','0110111','0001011'); $Rencode = array('1110010','1100110','1101100','1000010','1011100', '1001110','1010000','1000100','1001000','1110100'); $ends = '101'; $center = '01010'; /* UPC-A Must be 11 digits, we compute the checksum. */ if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); } /* Compute the EAN-13 Checksum digit */ $ncode = '0'.$code; $even = 0; $odd = 0; for ($x=0;$x<12;$x++) { if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; } } $code.=(10 - (($odd * 3 + $even) % 10)) % 10; /* Create the bar encoding using a binary string */ $bars=$ends; $bars.=$Lencode[$code[0]]; for($x=1;$x<6;$x++) { $bars.=$Lencode[$code[$x]]; } $bars.=$center; for($x=6;$x<12;$x++) { $bars.=$Rencode[$code[$x]]; } $bars.=$ends; /* Generate the Barcode Image */ $img = ImageCreate($lw*95+30,$hi+30); $fg = ImageColorAllocate($img, 0, 0, 0); $bg = ImageColorAllocate($img, 255, 255, 255); ImageFilledRectangle($img, 0, 0, $lw*95+30, $hi+30, $bg); $shift=10; for ($x=0;$x<strlen($bars);$x++) { if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; } if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; } ImageFilledRectangle($img, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color); } /* Add the Human Readable Label */ ImageString($img,4,5,$hi-5,$code[0],$fg); for ($x=0;$x<5;$x++) { ImageString($img,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg); ImageString($img,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg); } ImageString($img,4,$lw*95+17,$hi-5,$code[11],$fg); /* Output the Header and Content. */ header("Content-Type: image/png"); ImagePNG($img); } ?>
由于二維碼允許有一定的容錯(cuò)性,一般的二維碼即使在遮住部分但仍然能夠解碼,經(jīng)常我們掃描二維碼的時(shí)候掃描到甚至不到一半時(shí)就能解碼掃描結(jié)果,這是因?yàn)樯善鲿?huì)將部分信息重復(fù)表示來提高其容錯(cuò)度,這就是為什么我們?cè)诙S碼中間加個(gè)LOGO圖片并不影響解碼結(jié)果的原因。
以上通過兩種方法介紹php生成二維碼,希望能夠幫助到大家。
相關(guān)文章
php獲取用戶真實(shí)IP和防刷機(jī)制的實(shí)例代碼
這篇文章主要介紹了php獲取用戶真實(shí)IP和防刷機(jī)制,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-11-11ThinkPHP模板之變量輸出、自定義函數(shù)與判斷語句用法
這篇文章主要介紹了ThinkPHP模板之變量輸出、自定義函數(shù)與判斷語句用法,是關(guān)于ThinkPHP模板操作中非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11yii2使用ajax返回json的實(shí)現(xiàn)方法
這篇文章主要介紹了yii2使用ajax返回json的實(shí)現(xiàn)方法,實(shí)例分析了Yii框架使用ajax調(diào)用數(shù)據(jù)及返回json格式數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-05-05bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解
下面小編就為大家分享一篇bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03ThinkPHP+EasyUI之ComboTree中的會(huì)計(jì)科目樹形菜單實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猅hinkPHP+EasyUI之ComboTree中的會(huì)計(jì)科目樹形菜單實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06php使用FFmpeg接口獲取視頻的播放時(shí)長(zhǎng)、碼率、縮略圖以及創(chuàng)建時(shí)間
本篇文章主要介紹了php使用FFmpeg接口獲取視頻的播放時(shí)長(zhǎng)、碼率、縮略圖以及創(chuàng)建時(shí)間,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11thinkphp實(shí)現(xiàn)多語言功能(語言包)
這篇文章主要介紹了thinkphp實(shí)現(xiàn)多語言功能(語言包),需要的朋友可以參考下2014-03-03