The simplest and most practical goolge open source method
1.google open api
The code is as follows:
$urlToEncode="http://www.helloweba.com"; generateQRfromGoogle($urlToEncode); /** * google api 二維碼生成【QRcode可以存儲最多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ò)級別,QR碼支持四個(gè)等級糾錯(cuò),用來恢復(fù)丟失的、讀錯(cuò)的、模糊的、數(shù)據(jù)。 * L-默認(rèn):可以識別已損失的7%的數(shù)據(jù) * M-可以識別已損失15%的數(shù)據(jù) * Q-可以識別已損失25%的數(shù)據(jù) * H-可以識別已損失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.'"/>'; }
2.php class library PHP QR Code
##Address: http://phpqrcode.sourceforge.net/ Download: http://sourceforge.net/projects/phpqrcode/
The code is as follows:
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); }
Calling PHP QR Code:
include 'phpqrcode.php'; QRcode::png('http://www.helloweba.com');
In actual application, We will add our own LOGO in the middle of the QR code to enhance the publicity effect. So how to generate a QR code containing a logo? In fact, the principle is very simple. First use PHP QR Code to generate a QR code image, and then use PHP's image related function to add the pre-prepared logo image to the middle of the original QR code image just generated, and then regenerate a new one. QR code picture.
include 'phpqrcode.php'; $value = 'http://www.helloweba.com'; //二維碼內(nèi)容 $errorCorrectionLevel = 'L';//容錯(cuò)級別 $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, 'helloweba.png'); echo '<img src="helloweba.png">';
For the second method: If $filename is not used and the second parameter is false, the QR code image will not be saved, but will be output directly.
There are still some if you like: libqrencode and QRcode Perl CGI & PHP scripts QR code generation plug-in. If you like it, you can also take a look.
3. Based on jquery's QR code generation plug-in qrcode, the corresponding QR code can be generated by calling the plug-in on the page.
qrcode actually implements graphic rendering and drawing by using jQuery, and supports canvas (HTML5) and table.
You can go to https://github.com/jeromeetienne/jquery-qrcode to get the latest code. How to use
(1), first add the jquery library file and qrcode plug-in to the page.
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.qrcode.min.js"></script>
(2). Add the following code to the page where the QR code needs to be displayed:
<p id="code"></p>
(3). Call the qrcode plug-in.
qrcode supports two methods of canvas and table for image rendering. The canvas method is used by default, which is the most efficient. Of course, the browser must support html5. Directly call it as follows:
$('#code').qrcode("http://www.helloweba.com"); //任意字符串
You can also call it in the following way:
$("#code").qrcode({ render: "table", //table方式 width: 200, //寬度 height:200, //高度 text: "www.helloweba.com" //任意內(nèi)容 });
This will generate a QR code directly on the page, and you can use the "scan" function of your mobile phone to read it. Get the QR code information.
(4).識別中文
我們試驗(yàn)的時(shí)候發(fā)現(xiàn)不能識別中文內(nèi)容的二維碼,通過查找多方資料了解到,jquery-qrcode是采用charCodeAt()方式進(jìn)行編碼轉(zhuǎn)換的。而這個(gè)方法默認(rèn)會獲取它的Unicode編碼,如果有中文內(nèi)容,在生成二維碼前就要把字符串轉(zhuǎn)換成UTF-8,然后再生成二維碼。您可以通過以下函數(shù)來轉(zhuǎn)換中文字符串:
function toUtf8(str) { var out, i, len, c; out = ""; len = str.length; for(i = 0; i < len; i++) { c = str.charCodeAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { out += str.charAt(i); } else if (c > 0x07FF) { out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } else { out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } } return out; }
以下示例:
var str = toUtf8("普羅旺斯沒有故事"); $('#code').qrcode(str);
本文介紹了php生成二維碼的三種方法,更多相關(guān)內(nèi)容請關(guān)注php中文網(wǎng)。
相關(guān)推薦:
講解php 支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類的相關(guān)內(nèi)容
The above is the detailed content of Three ways to generate QR codes in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.
