隨著科技的發(fā)展,二維碼無(wú)處不在,特別是在智能手機(jī)時(shí)代二維碼更活躍,下面小編通過(guò)兩種方法給大家介紹利用php生成二維碼,需要的朋友可以參考下
隨著科技的進(jìn)步,大家經(jīng)常見(jiàn)到條形碼和二維碼,特別是智能手機(jī)時(shí)代這些碼更顯得活躍了,今天我給大家分享下如何使用PHP生成二維碼,至于如何生成二維碼,一般常用的試調(diào)用google的開(kāi)放api來(lái)生成,言歸正傳,直接上代碼了:
第一種方法:
利用Google API生成二維碼
Google提供了較為完善的二維碼生成接口,調(diào)用API接口很簡(jiǎn)單,以下是調(diào)用代碼:
- $urlToEncode="http://m.survivalescaperooms.com";
- generateQRfromGoogle($urlToEncode);
- /**
- * google api 二維碼生成【QRcode可以存儲(chǔ)最多4296個(gè)字母數(shù)字類型的任意文本,具體可以查看二維碼數(shù)據(jù)格式】
- * @param string $chl 二維碼包含的信息,可以是數(shù)字、字符、二進(jìn)制信息、漢字。
- 不能混合數(shù)據(jù)類型,數(shù)據(jù)必須經(jīng)過(guò)UTF-8 URL-encoded
- * @param int $widhtHeight 生成二維碼的尺寸設(shè)置
- * @param string $EC_level 可選糾錯(cuò)級(jí)別,QR碼支持四個(gè)等級(jí)糾錯(cuò),用來(lái)恢復(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二維碼生成類庫(kù)PHP QR Code生成二維碼
PHP QR Code
是一個(gè)PHP二維碼生成類庫(kù),利用它可以輕松生成二維碼,官網(wǎng)提供了下載和多個(gè)演示demo,查看地址:http://phpqrcode.sourceforge.net/。
phpqrcode.php
下載官網(wǎng)提供的類庫(kù)后,只需要使用就可以生成二維碼了,當(dāng)然您的PHP環(huán)境必須開(kāi)啟支持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表示二維碼周?chē)吙蚩瞻讌^(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://m.survivalescaperooms.com"的二維碼.
- include 'phpqrcode.php';
- QRcode::png('http://m.survivalescaperooms.com');
那么實(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://m.survivalescaperooms.com'; //二維碼內(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ù)表示來(lái)提高其容錯(cuò)度,這就是為什么我們?cè)诙S碼中間加個(gè)LOGO圖片并不影響解碼結(jié)果的原因。
以上通過(guò)兩種方法介紹php生成二維碼,希望能夠幫助到大家。
新聞熱點(diǎn)
疑難解答