Table of Contents
The functions are as follows:
Settings Parameter description:
The code is as follows:
Home Backend Development PHP Tutorial PHP implements QR code class with logo

PHP implements QR code class with logo

Mar 26, 2018 pm 02:52 PM
logo php

This article mainly introduces you to the php implementation of creating a QR code class, which supports setting size, adding LOGO, stroke, rounded corners, transparency, and other processing. Complete code, demonstration examples and detailed parameter descriptions are provided to facilitate everyone's learning and use. Hope it helps everyone.

The functions are as follows:

1. Create a QR code
2. Add the logo to the QR code
3. The logo can be stroked
4. The logo can be Rounded corners
5. The transparency of the logo can be set
6. The logo image and output image type support png, jpg, gif formats
7. The output image quality can be set

Settings Parameter description:

ecc
QR code quality L-smallest, M, Q, H-best

size
QR code size 1-50

dest_file
Generated QR code image path

quality
Generated image quality

logo
logo path, empty means not to add logo

logo_size
logo size, null means press QR code The size ratio is automatically calculated

logo_outline_size
The logo stroke size, null means it is automatically calculated in proportion to the logo size

logo_outline_color
logo stroke color

logo_opacity
logo opacity 0-100

logo_radius
logo fillet angle 0-30

The code is as follows:

PHPQRCode.class.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

<?phprequire_once dirname(__FILE__)."/qrcode/qrlib.php";/**

 * PHP创建二维码类

 * Date:    2018-03-18

 * Author:  fdipzone

 * Version: 1.0

 *

 * Description:

 * PHP实现创建二维码类,支持设置尺寸,加入LOGO,圆角,透明度,等处理。

 *

 * Func:

 * public  set_config           设定配置

 * public  generate             创建二维码

 * private create_qrcode        创建纯二维码图片

 * private add_logo             合拼纯二维码图片与logo图片

 * private image_outline        图片对象进行描边

 * private image_fillet         图片对象进行圆角处理

 * private imagecopymerge_alpha 合拼图片并保留各自透明度

 * private create_dirs          创建目录

 * private hex2rgb              hex颜色转rgb颜色

 * private get_file_ext         获取图片类型

 */class PHPQRCode{ // class start

 

    /** 默认设定 */

    private $_config = array(        &#39;ecc&#39; => &#39;H&#39;,                       // 二维码质量 L-smallest, M, Q, H-best

        &#39;size&#39; => 15,                       // 二维码尺寸 1-50

        &#39;dest_file&#39; => &#39;qrcode.png&#39;,        // 创建的二维码路径

        &#39;quality&#39; => 100,                    // 图片质量

        &#39;logo&#39; => &#39;&#39;,                       // logo路径,为空表示没有logo

        &#39;logo_size&#39; => null,                // logo尺寸,null表示按二维码尺寸比例自动计算

        &#39;logo_outline_size&#39; => null,        // logo描边尺寸,null表示按logo尺寸按比例自动计算

        &#39;logo_outline_color&#39; => &#39;#FFFFFF&#39;,  // logo描边颜色

        &#39;logo_opacity&#39; => 100,              // logo不透明度 0-100

        &#39;logo_radius&#39; => 0,                 // logo圆角角度 0-30

    );    /**

     * 设定配置

     * @param  Array   $config 配置内容

     */

    public function set_config($config){

 

        // 允许设定的配置

        $config_keys = array_keys($this->_config);        // 获取传入的配置,写入设定

        foreach($config_keys as $k=>$v){            if(isset($config[$v])){                $this->_config[$v] = $config[$v];

            }

        }

 

    }    /**

     * 创建二维码

     * @param  String $data 二维码内容

     * @return String

     */

    public function generate($data){

 

        // 创建临时二维码图片

        $tmp_qrcode_file = $this->create_qrcode($data);        // 合拼临时二维码图片与logo图片

        $this->add_logo($tmp_qrcode_file);        // 删除临时二维码图片

        if($tmp_qrcode_file!=&#39;&#39; && file_exists($tmp_qrcode_file)){

            unlink($tmp_qrcode_file);

        }        return file_exists($this->_config[&#39;dest_file&#39;])? $this->_config[&#39;dest_file&#39;] : &#39;&#39;;

 

    }    /**

     * 创建临时二维码图片

     * @param  String $data 二维码内容

     * @return String

     */

    private function create_qrcode($data){

 

        // 临时二维码图片

        $tmp_qrcode_file = dirname(__FILE__).&#39;/tmp_qrcode_&#39;.time().mt_rand(100,999).&#39;.png&#39;;        // 创建临时二维码

        QRcode::png($data, $tmp_qrcode_file, $this->_config[&#39;ecc&#39;], $this->_config[&#39;size&#39;], 2);        // 返回临时二维码路径

        return file_exists($tmp_qrcode_file)? $tmp_qrcode_file : &#39;&#39;;

 

    }    /**

     * 合拼临时二维码图片与logo图片

     * @param String $tmp_qrcode_file 临时二维码图片

     */

    private function add_logo($tmp_qrcode_file){

 

        // 创建目标文件夹

        $this->create_dirs(dirname($this->_config[&#39;dest_file&#39;]));        // 获取目标图片的类型

        $dest_ext = $this->get_file_ext($this->_config[&#39;dest_file&#39;]);        // 需要加入logo

        if(file_exists($this->_config[&#39;logo&#39;])){            // 创建临时二维码图片对象

            $tmp_qrcode_img = imagecreatefrompng($tmp_qrcode_file);            // 获取临时二维码图片尺寸

            list($qrcode_w, $qrcode_h, $qrcode_type) = getimagesize($tmp_qrcode_file);            // 获取logo图片尺寸及类型

            list($logo_w, $logo_h, $logo_type) = getimagesize($this->_config[&#39;logo&#39;]);            // 创建logo图片对象

            switch($logo_type){ 

                case 1: $logo_img = imagecreatefromgif($this->_config[&#39;logo&#39;]); break

                case 2: $logo_img = imagecreatefromjpeg($this->_config[&#39;logo&#39;]); break

                case 3: $logo_img = imagecreatefrompng($this->_config[&#39;logo&#39;]); break

                default: return &#39;&#39;; 

            }            // 设定logo图片合拼尺寸,没有设定则按比例自动计算

            $new_logo_w = isset($this->_config[&#39;logo_size&#39;])? $this->_config[&#39;logo_size&#39;] : (int)($qrcode_w/5);            $new_logo_h = isset($this->_config[&#39;logo_size&#39;])? $this->_config[&#39;logo_size&#39;] : (int)($qrcode_h/5);            // 按设定尺寸调整logo图片

            $new_logo_img = imagecreatetruecolor($new_logo_w, $new_logo_h);

            imagecopyresampled($new_logo_img, $logo_img, 0, 0, 0, 0, $new_logo_w, $new_logo_h, $logo_w, $logo_h);            // 判断是否需要描边

            if(!isset($this->_config[&#39;logo_outline_size&#39;]) || $this->_config[&#39;logo_outline_size&#39;]>0){                list($new_logo_img, $new_logo_w, $new_logo_h) = $this->image_outline($new_logo_img);

            }            // 判断是否需要圆角处理

            if($this->_config[&#39;logo_radius&#39;]>0){                $new_logo_img = $this->image_fillet($new_logo_img);

            }            // 合拼logo与临时二维码

            $pos_x = ($qrcode_w-$new_logo_w)/2;            $pos_y = ($qrcode_h-$new_logo_h)/2;

 

            imagealphablending($tmp_qrcode_img, true);            // 合拼图片并保留各自透明度

            $dest_img = $this->imagecopymerge_alpha($tmp_qrcode_img, $new_logo_img, $pos_x, $pos_y, 0, 0, $new_logo_w, $new_logo_h, $this->_config[&#39;logo_opacity&#39;]);            // 生成图片

            switch($dest_ext){                case 1: imagegif($dest_img, $this->_config[&#39;dest_file&#39;], $this->_config[&#39;quality&#39;]); break;                case 2: imagejpeg($dest_img, $this->_config[&#39;dest_file&#39;], $this->_config[&#39;quality&#39;]); break;                case 3: imagepng($dest_img, $this->_config[&#39;dest_file&#39;], (int)(($this->_config[&#39;quality&#39;]-1)/10)); break;

            }

 

        // 不需要加入logo

        }else{            $dest_img = imagecreatefrompng($tmp_qrcode_file);            // 生成图片

            switch($dest_ext){                case 1: imagegif($dest_img, $this->_config[&#39;dest_file&#39;], $this->_config[&#39;quality&#39;]); break;                case 2: imagejpeg($dest_img, $this->_config[&#39;dest_file&#39;], $this->_config[&#39;quality&#39;]); break;                case 3: imagepng($dest_img, $this->_config[&#39;dest_file&#39;], (int)(($this->_config[&#39;quality&#39;]-1)/10)); break;

            }

        }

 

    }    /**

     * 对图片对象进行描边

     * @param  Obj   $img 图片对象

     * @return Array

     */

    private function image_outline($img){

 

        // 获取图片宽高

        $img_w = imagesx($img);        $img_h = imagesy($img);        // 计算描边尺寸,没有设定则按比例自动计算

        $bg_w = isset($this->_config[&#39;logo_outline_size&#39;])? intval($img_w + $this->_config[&#39;logo_outline_size&#39;]) : $img_w + (int)($img_w/5);        $bg_h = isset($this->_config[&#39;logo_outline_size&#39;])? intval($img_h + $this->_config[&#39;logo_outline_size&#39;]) : $img_h + (int)($img_h/5);        // 创建底图对象

        $bg_img = imagecreatetruecolor($bg_w, $bg_h);        // 设置底图颜色

        $rgb = $this->hex2rgb($this->_config[&#39;logo_outline_color&#39;]);        $bgcolor = imagecolorallocate($bg_img, $rgb[&#39;r&#39;], $rgb[&#39;g&#39;], $rgb[&#39;b&#39;]);        // 填充底图颜色

        imagefill($bg_img, 0, 0, $bgcolor);        // 合拼图片与底图,实现描边效果

        imagecopy($bg_img, $img, (int)(($bg_w-$img_w)/2), (int)(($bg_h-$img_h)/2), 0, 0, $img_w, $img_h);        $img = $bg_img;        return array($img, $bg_w, $bg_h);

 

    }    /**

     * 对图片对象进行圆角处理

     * @param  Obj $img 图片对象

     * @return Obj

     */

    private function image_fillet($img){

 

        // 获取图片宽高

        $img_w = imagesx($img);        $img_h = imagesy($img);        // 创建圆角图片对象

        $new_img = imagecreatetruecolor($img_w, $img_h);        // 保存透明通道

        imagesavealpha($new_img, true);        // 填充圆角图片

        $bg = imagecolorallocatealpha($new_img, 255, 255, 255, 127);

        imagefill($new_img, 0, 0, $bg);        // 圆角半径

        $r = $this->_config[&#39;logo_radius&#39;];        // 执行圆角处理

        for($x=0; $x<$img_w; $x++){            for($y=0; $y<$img_h; $y++){                $rgb = imagecolorat($img, $x, $y);                // 不在图片四角范围,直接画图

                if(($x>=$r && $x<=($img_w-$r)) || ($y>=$r && $y<=($img_h-$r))){

                    imagesetpixel($new_img, $x, $y, $rgb);                // 在图片四角范围,选择画图

                }else{                    // 上左

                    $ox = $r; // 圆心x坐标

                    $oy = $r; // 圆心y坐标

                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){

                        imagesetpixel($new_img, $x, $y, $rgb);

                    }                    // 上右

                    $ox = $img_w-$r; // 圆心x坐标

                    $oy = $r;        // 圆心y坐标

                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){

                        imagesetpixel($new_img, $x, $y, $rgb);

                    }                    // 下左

                    $ox = $r;        // 圆心x坐标

                    $oy = $img_h-$r; // 圆心y坐标

                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){

                        imagesetpixel($new_img, $x, $y, $rgb);

                    }                    // 下右

                    $ox = $img_w-$r; // 圆心x坐标

                    $oy = $img_h-$r; // 圆心y坐标

                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){

                        imagesetpixel($new_img, $x, $y, $rgb);

                    }

 

                }

 

            }

        }        return $new_img;

 

    }    // 合拼图片并保留各自透明度

    private function imagecopymerge_alpha($dest_img, $src_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity){

 

        $w = imagesx($src_img);        $h = imagesy($src_img);        $tmp_img = imagecreatetruecolor($src_w, $src_h);

 

        imagecopy($tmp_img, $dest_img, 0, 0, $pos_x, $pos_y, $src_w, $src_h);

        imagecopy($tmp_img, $src_img, 0, 0, $src_x, $src_y, $src_w, $src_h);

        imagecopymerge($dest_img, $tmp_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity);        return $dest_img;

 

    }    /**

     * 创建目录

     * @param  String  $path

     * @return Boolean

     */

    private function create_dirs($path){

 

        if(!is_dir($path)){            return mkdir($path, 0777, true);

        }        return true;

 

    }    /** hex颜色转rgb颜色

     *  @param  String $color hex颜色

     *  @return Array

     */

    private function hex2rgb($hexcolor){

        $color = str_replace(&#39;#&#39;, &#39;&#39;, $hexcolor);        if (strlen($color) > 3) {            $rgb = array(                &#39;r&#39; => hexdec(substr($color, 0, 2)),                &#39;g&#39; => hexdec(substr($color, 2, 2)),                &#39;b&#39; => hexdec(substr($color, 4, 2))

            );

        } else {            $r = substr($color, 0, 1) . substr($color, 0, 1);            $g = substr($color, 1, 1) . substr($color, 1, 1);            $b = substr($color, 2, 1) . substr($color, 2, 1);            $rgb = array(                &#39;r&#39; => hexdec($r),                &#39;g&#39; => hexdec($g),                &#39;b&#39; => hexdec($b)

            );

        }        return $rgb;

    }    /** 获取图片类型

     * @param  String $file 图片路径

     * @return int

     */ 

    private function get_file_ext($file){

        $filename = basename($file);        list($name, $ext)= explode(&#39;.&#39;, $filename);        $ext_type = 0;        switch(strtolower($ext)){            case &#39;jpg&#39;:            case &#39;jpeg&#39;:                $ext_type = 2;                break;            case &#39;gif&#39;:                $ext_type = 1;                break;            case &#39;png&#39;:                $ext_type = 3;                break;

        }        return $ext_type;

    }

 

} // class end?>

Copy after login

demo.php

1

2

3

4

<?phprequire &#39;PHPQRCode.class.php&#39;;$config = array(        &#39;ecc&#39; => &#39;H&#39;,    // L-smallest, M, Q, H-best

        &#39;size&#39; => 12,    // 1-50

        &#39;dest_file&#39; => &#39;qrcode.png&#39;,        &#39;quality&#39; => 90,        &#39;logo&#39; => &#39;logo.jpg&#39;,        &#39;logo_size&#39; => 100,        &#39;logo_outline_size&#39; => 20,        &#39;logo_outline_color&#39; => &#39;#FFFF00&#39;,        &#39;logo_radius&#39; => 15,        &#39;logo_opacity&#39; => 100,

);// 二维码内容$data = &#39;http://weibo.com/fdipzone&#39;;// 创建二维码类$oPHPQRCode = new PHPQRCode();// 设定配置$oPHPQRCode->set_config($config);// 创建二维码$qrcode = $oPHPQRCode->generate($data);// 显示二维码echo &#39;<img src="&#39;.$qrcode.&#39;?t=&#39;.time().&#39;">&#39;;?>

Copy after login

Related recommendations:

(Advanced) Summary of how to generate QR code with logo in PHP

The above is the detailed content of PHP implements QR code class with logo. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles