使用PHP生成二维码的两种方法(带logo图像)_php实例
一、利用Google API生成二维码
Google提供了较为完善的二维码生成接口,调用API接口很简单,以下是调用代码:
$urlToEncode="http://www.php.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 '<img src="/static/imghw/default1.png" data-src="helloweba.png" class="lazy".$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是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo,查看地址:http://phpqrcode.sourceforge.net/。
下载官网提供的类库后,只需要使用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.php.net"的二维码.
Php代码
include 'phpqrcode.php';
QRcode::png('http://www.php.net');
那么实际应用中,我们会在二维码的中间加上自己的LOGO,已增强宣传效果。那如何生成含有logo的二维码呢?其实原理很简单,先使用PHP QR Code生成一张二维码图片,然后再利用php的image相关函数,将事先准备好的logo图片加入到刚生成的原始二维码图片中间,然后重新生成一张新 的二维码图片。
include 'phpqrcode.php'; $value = 'http://www.php.net'; //二维码内容 $errorCorrectionLevel = 'L';//容错级别 $matrixPointSize = 6;//生成图片大小 //生成二维码图片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'logo.png';//准备好的logo图片 $QR = 'qrcode.png';//已经生成的原始二维码图 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, 'helloweba.png'); echo '<img src="/static/imghw/default1.png" data-src="helloweba.png" class="lazy" alt="使用PHP生成二维码的两种方法(带logo图像)_php实例" >';
下面是参考上面的代码,不生产图片文件,方便调用的,将下面的代码保存为img.php
<?php include 'phpqrcode.php'; $value = $_GET['url'];//二维码内容 $errorCorrectionLevel = 'L';//容错级别 $matrixPointSize = 6;//生成图片大小 //生成二维码图片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'php.png';//准备好的logo图片 $QR = 'qrcode.png';//已经生成的原始二维码图 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图片并不影响解码结果的原因。
PS:本站还提供了一个功能十分强大的二维码工具供大家使用:
http://tools.php.net/transcoding/phpqrcode

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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 this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
