search
Homephp教程PHP源码使用PHP生成二维码的两种方法(带logo图像)_php实例

随着科技的进步,二维码应用领域越来越广泛,今天我给大家分享下如何使用PHP生成二维码,以及如何生成中间带LOGO图像的二维码

一、利用Google API生成二维码
 Google提供了较为完善的二维码生成接口,调用API接口很简单,以下是调用代码:

$urlToEncode="http://www.jb51.net"; 
generateQRfromGoogle($urlToEncode); 
/** 
 * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 
 * @param string $chl 二维码包含的信息,可以是数字、字符、二进制信息、汉字。 
 不能混合数据类型,数据必须经过UTF-8 URL-encoded 
 * @param int $widhtHeight 生成二维码的尺寸设置 
 * @param string $EC_level 可选纠错级别,QR码支持四个等级纠错,用来恢复丢失的、读错的、模糊的、数据。 
 * L-默认:可以识别已损失的7%的数据 
 * M-可以识别已损失15%的数据 
 * Q-可以识别已损失25%的数据 
 * H-可以识别已损失30%的数据 
 * @param int $margin 生成的二维码离图片边框的距离 
 */ 
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') 
{ 
 $chl = urlencode($chl); 
 echo &#39;<img src="http://chart.apis.google.com/chart?chs=&#39;.$widhtHeight.&#39;x&#39;.$widhtHeight.&#39; 
 &cht=qr&chld=&#39;.$EC_level.&#39;|&#39;.$margin.&#39;&chl=&#39;.$chl.&#39;" alt="QR code" widhtHeight="&#39;.$widhtHeight.&#39; 
 " widhtHeight="&#39;.$widhtHeight.&#39;"/>&#39;; 
}


二、使用PHP二维码生成类库PHP QR Code生成二维码
 
PHP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo,查看地址:http://www.php.cn/。
下载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2。 phpqrcode.php提供了一个关键的png()方法,其中参数$text表示生成二位的的信息文本;参数$outfile表示是否输出二维码图片 文件,默认否;参数$level表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%); 参数$size表示生成图片大小,默认是3;参数$margin表示二维码周围边框空白区域间距值;参数$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); 
}

调用PHP QR Code非常简单,如下代码即可生成一张内容为"http://www.jb51.net"的二维码.
Php代码 
include 'phpqrcode.php';  
QRcode::png('http://www.jb51.net');  
 
那么实际应用中,我们会在二维码的中间加上自己的LOGO,已增强宣传效果。那如何生成含有logo的二维码呢?其实原理很简单,先使用PHP QR Code生成一张二维码图片,然后再利用php的image相关函数,将事先准备好的logo图片加入到刚生成的原始二维码图片中间,然后重新生成一张新 的二维码图片。

include &#39;phpqrcode.php&#39;; 
$value = &#39;http://www.jb51.net&#39;; //二维码内容 
$errorCorrectionLevel = &#39;L&#39;;//容错级别 
$matrixPointSize = 6;//生成图片大小 
//生成二维码图片 
QRcode::png($value, &#39;qrcode.png&#39;, $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = &#39;logo.png&#39;;//准备好的logo图片 
$QR = &#39;qrcode.png&#39;;//已经生成的原始二维码图 
 
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; 
 //重新组合图片并调整大小 
 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, 
 $logo_qr_height, $logo_width, $logo_height); 
} 
//输出图片 
imagepng($QR, &#39;helloweba.png&#39;); 
echo &#39;<img src="helloweba.png">&#39;;


下面是参考上面的代码,不生产图片文件,方便调用的,将下面的代码保存为img.php

<?php
include &#39;phpqrcode.php&#39;; 
$value = $_GET[&#39;url&#39;];//二维码内容 
$errorCorrectionLevel = &#39;L&#39;;//容错级别 
$matrixPointSize = 6;//生成图片大小 
//生成二维码图片 
QRcode::png($value, &#39;qrcode.png&#39;, $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = &#39;jb51.png&#39;;//准备好的logo图片 
$QR = &#39;qrcode.png&#39;;//已经生成的原始二维码图 

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; 
 //重新组合图片并调整大小 
 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);

调用方法:



由于二维码允许有一定的容错性,一般的二维码即使在遮住部分但仍然能够解码,经常我们扫描二维码的时候扫描到甚至不到一半时就能解码扫描结果,这是因为生成器会将部分信息重复表示来提高其容错度,这就是为什么我们在二维码中间加个LOGO图片并不影响解码结果的原因。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.