Home > Article > Backend Development > PHP generates QR code and adds logo between QR images_PHP tutorial
When I wrote the QR code two years ago, no one knew it. Later, there were more software for scanning QR codes on mobile phones, and WeChat also changed this era.
Thanks to smartphones and WeChat.
================================================== ============
QR code is a type of two-dimensional barcode that can compile information such as website addresses, text, photos, etc. into a square barcode pattern through corresponding encoding algorithms. Mobile phone users can re-decode the relevant information and view the content through the camera and decoding software.
Two-dimensional bar code (dimensional bar code) uses a specific geometric figure to record data symbol information in black and white graphics distributed on a plane (in a two-dimensional direction) according to certain rules; it is cleverly used in code compilation. The concept of "0" and "1" bit streams, which form the basis of computer internal logic, uses several geometric shapes corresponding to binary to represent text numerical information, and automatically reads the information through image input equipment or photoelectric scanning equipment to achieve automatic information Processing: It has some common features of barcode technology: each code system has its own specific character set; each character occupies a certain width; it has certain verification functions, etc. At the same time, it also has the features of automatically identifying different lines of information and processing graphics rotation changes. Among many types of two-dimensional barcodes, commonly used code systems include: Data Matrix, Maxi Code, Aztec, QR Code, Vericode, PDF417, Ultracode, Code 49, Code 16K, etc. The QR code was developed by the Japanese company Denso-Wave in 1994. invention. QR comes from the abbreviation of "Quick Response" in English, which means quick response. It comes from the inventor's hope that the content of QR codes can be decoded quickly. QR codes are most common in Japan and South Korea; they are currently the most popular two-dimensional barcode in Japan.
<?php /** * php生成二维码的2种方式 * @author insun */ //1.google开放api $urlToEncode="smart_insun"; 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="'.$widhtHeight.'" widhtHeight="'.$widhtHeight.'"/>'; } //2.php类库PHP QR Code:http://phpqrcode.sourceforge.net/ /* $data 数据 $filename 保存的图片名称 $errorCorrectionLevel 错误处理级别 $matrixPointSize 每个黑点的像素 $margin 图片外围的白色边框像素 */ include "phpqrcode/qrlib.php"; $data = "insun"; $filename ="QR.jpg"; $errorCorrectionLevel = 'L'; $matrixPointSize = 2; $margin = 5; QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, $margin);//这样就生成了QR.jpg
QR图片中间加logo
<?php /** * QR Code + Logo Generator QR图片中间加logo,QR是根据google开放api生成的,其实啥都没有 * * http://labs.nticompassinc.com */ //ini_set("auto_detect_line_endings", true); $data = isset($_GET['data']) ? $_GET['data'] : 'http://weixin.qq.com/r/8bxsY6LEqpzVh7MAn_nV'; $size = isset($_GET['size']) ? $_GET['size'] : '200x200'; $logo = isset($_GET['logo']) ? $_GET['logo'] : './logo.jpg';//中间那logo图 // Get QR Code image from Google Chart API // http://code.google.com/apis/chart/infographics/docs/qr_codes.html //https://chart.googleapis.com/chart?cht=qr&chld=H|1&chs='.$size.'&chl='.urlencode($data)); $png = "http://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=Hello+world&chld=L|1&choe=UTF-8"; $QR = imagecreatefrompng($png);//Warning: imagecreatefrompng() [function.imagecreatefrompng]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? = //$QR = imagecreatefrompng('./chart.png');//外面那QR图 if($logo !== FALSE){ $logo = imagecreatefromstring(file_get_contents($logo)); $QR_width = imagesx($QR); $QR_height = imagesy($QR); $logo_width = imagesx($logo); $logo_height = imagesy($logo); // Scale logo to fit in the QR Code $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; //echo $from_width;exit; imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } header('Content-type: image/png'); imagepng($QR); imagedestroy($QR); ?>