当前位置:Gxlcms > PHP教程 > php实现带logo二维码类

php实现带logo二维码类

时间:2021-07-01 10:21:17 帮助过:22人阅读

本文主要和大家介绍php实现创建二维码类,支持设置尺寸,加入LOGO,描边、圆角、透明度,等处理。提供完整代码,演示实例及详细参数说明,方便大家学习使用。 希望能帮助到大家。

实现功能如下:

1.创建二维码
2.加入logo到二维码中
3.logo可描边
4.logo可圆角
5.logo可设透明度
6.logo图片及输出图片类型支持png,jpg,gif格式
7.可设置输出图片质量

设定参数说明:

ecc
二维码质量 L-smallest, M, Q, H-best

size
二维码尺寸 1-50

dest_file
生成的二维码图片路径

quality
生成的图片质量

logo
logo路径,为空表示不加入logo

logo_size
logo尺寸,null表示按二维码尺寸比例自动计算

logo_outline_size
logo描边尺寸,null表示按logo尺寸按比例自动计算

logo_outline_color
logo描边颜色

logo_opacity
logo不透明度 0-100

logo_radius
logo圆角角度 0-30

代码如下:

PHPQRCode.class.php

  1. <?phprequire_once dirname(__FILE__)."/qrcode/qrlib.php";/**
  2. * PHP创建二维码类
  3. * Date: 2018-03-18
  4. * Author: fdipzone
  5. * Version: 1.0
  6. *
  7. * Description:
  8. * PHP实现创建二维码类,支持设置尺寸,加入LOGO,圆角,透明度,等处理。
  9. *
  10. * Func:
  11. * public set_config 设定配置
  12. * public generate 创建二维码
  13. * private create_qrcode 创建纯二维码图片
  14. * private add_logo 合拼纯二维码图片与logo图片
  15. * private image_outline 图片对象进行描边
  16. * private image_fillet 图片对象进行圆角处理
  17. * private imagecopymerge_alpha 合拼图片并保留各自透明度
  18. * private create_dirs 创建目录
  19. * private hex2rgb hex颜色转rgb颜色
  20. * private get_file_ext 获取图片类型
  21. */class PHPQRCode{ // class start
  22. /** 默认设定 */
  23. private $_config = array( 'ecc' => 'H', // 二维码质量 L-smallest, M, Q, H-best
  24. 'size' => 15, // 二维码尺寸 1-50
  25. 'dest_file' => 'qrcode.png', // 创建的二维码路径
  26. 'quality' => 100, // 图片质量
  27. 'logo' => '', // logo路径,为空表示没有logo
  28. 'logo_size' => null, // logo尺寸,null表示按二维码尺寸比例自动计算
  29. 'logo_outline_size' => null, // logo描边尺寸,null表示按logo尺寸按比例自动计算
  30. 'logo_outline_color' => '#FFFFFF', // logo描边颜色
  31. 'logo_opacity' => 100, // logo不透明度 0-100
  32. 'logo_radius' => 0, // logo圆角角度 0-30
  33. ); /**
  34. * 设定配置
  35. * @param Array $config 配置内容
  36. */
  37. public function set_config($config){
  38. // 允许设定的配置
  39. $config_keys = array_keys($this->_config); // 获取传入的配置,写入设定
  40. foreach($config_keys as $k=>$v){ if(isset($config[$v])){ $this->_config[$v] = $config[$v];
  41. }
  42. }
  43. } /**
  44. * 创建二维码
  45. * @param String $data 二维码内容
  46. * @return String
  47. */
  48. public function generate($data){
  49. // 创建临时二维码图片
  50. $tmp_qrcode_file = $this->create_qrcode($data); // 合拼临时二维码图片与logo图片
  51. $this->add_logo($tmp_qrcode_file); // 删除临时二维码图片
  52. if($tmp_qrcode_file!='' && file_exists($tmp_qrcode_file)){
  53. unlink($tmp_qrcode_file);
  54. } return file_exists($this->_config['dest_file'])? $this->_config['dest_file'] : '';
  55. } /**
  56. * 创建临时二维码图片
  57. * @param String $data 二维码内容
  58. * @return String
  59. */
  60. private function create_qrcode($data){
  61. // 临时二维码图片
  62. $tmp_qrcode_file = dirname(__FILE__).'/tmp_qrcode_'.time().mt_rand(100,999).'.png'; // 创建临时二维码
  63. QRcode::png($data, $tmp_qrcode_file, $this->_config['ecc'], $this->_config['size'], 2); // 返回临时二维码路径
  64. return file_exists($tmp_qrcode_file)? $tmp_qrcode_file : '';
  65. } /**
  66. * 合拼临时二维码图片与logo图片
  67. * @param String $tmp_qrcode_file 临时二维码图片
  68. */
  69. private function add_logo($tmp_qrcode_file){
  70. // 创建目标文件夹
  71. $this->create_dirs(dirname($this->_config['dest_file'])); // 获取目标图片的类型
  72. $dest_ext = $this->get_file_ext($this->_config['dest_file']); // 需要加入logo
  73. if(file_exists($this->_config['logo'])){ // 创建临时二维码图片对象
  74. $tmp_qrcode_img = imagecreatefrompng($tmp_qrcode_file); // 获取临时二维码图片尺寸
  75. list($qrcode_w, $qrcode_h, $qrcode_type) = getimagesize($tmp_qrcode_file); // 获取logo图片尺寸及类型
  76. list($logo_w, $logo_h, $logo_type) = getimagesize($this->_config['logo']); // 创建logo图片对象
  77. switch($logo_type){
  78. case 1: $logo_img = imagecreatefromgif($this->_config['logo']); break;
  79. case 2: $logo_img = imagecreatefromjpeg($this->_config['logo']); break;
  80. case 3: $logo_img = imagecreatefrompng($this->_config['logo']); break;
  81. default: return '';
  82. } // 设定logo图片合拼尺寸,没有设定则按比例自动计算
  83. $new_logo_w = isset($this->_config['logo_size'])? $this->_config['logo_size'] : (int)($qrcode_w/5); $new_logo_h = isset($this->_config['logo_size'])? $this->_config['logo_size'] : (int)($qrcode_h/5); // 按设定尺寸调整logo图片
  84. $new_logo_img = imagecreatetruecolor($new_logo_w, $new_logo_h);
  85. imagecopyresampled($new_logo_img, $logo_img, 0, 0, 0, 0, $new_logo_w, $new_logo_h, $logo_w, $logo_h); // 判断是否需要描边
  86. if(!isset($this->_config['logo_outline_size']) || $this->_config['logo_outline_size']>0){ list($new_logo_img, $new_logo_w, $new_logo_h) = $this->image_outline($new_logo_img);
  87. } // 判断是否需要圆角处理
  88. if($this->_config['logo_radius']>0){ $new_logo_img = $this->image_fillet($new_logo_img);
  89. } // 合拼logo与临时二维码
  90. $pos_x = ($qrcode_w-$new_logo_w)/2; $pos_y = ($qrcode_h-$new_logo_h)/2;
  91. imagealphablending($tmp_qrcode_img, true); // 合拼图片并保留各自透明度
  92. $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['logo_opacity']); // 生成图片
  93. switch($dest_ext){ case 1: imagegif($dest_img, $this->_config['dest_file'], $this->_config['quality']); break; case 2: imagejpeg($dest_img, $this->_config['dest_file'], $this->_config['quality']); break; case 3: imagepng($dest_img, $this->_config['dest_file'], (int)(($this->_config['quality']-1)/10)); break;
  94. }
  95. // 不需要加入logo
  96. }else{ $dest_img = imagecreatefrompng($tmp_qrcode_file); // 生成图片
  97. switch($dest_ext){ case 1: imagegif($dest_img, $this->_config['dest_file'], $this->_config['quality']); break; case 2: imagejpeg($dest_img, $this->_config['dest_file'], $this->_config['quality']); break; case 3: imagepng($dest_img, $this->_config['dest_file'], (int)(($this->_config['quality']-1)/10)); break;
  98. }
  99. }
  100. } /**
  101. * 对图片对象进行描边
  102. * @param Obj $img 图片对象
  103. * @return Array
  104. */
  105. private function image_outline($img){
  106. // 获取图片宽高
  107. $img_w = imagesx($img); $img_h = imagesy($img); // 计算描边尺寸,没有设定则按比例自动计算
  108. $bg_w = isset($this->_config['logo_outline_size'])? intval($img_w + $this->_config['logo_outline_size']) : $img_w + (int)($img_w/5); $bg_h = isset($this->_config['logo_outline_size'])? intval($img_h + $this->_config['logo_outline_size']) : $img_h + (int)($img_h/5); // 创建底图对象
  109. $bg_img = imagecreatetruecolor($bg_w, $bg_h); // 设置底图颜色
  110. $rgb = $this->hex2rgb($this->_config['logo_outline_color']); $bgcolor = imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']); // 填充底图颜色
  111. imagefill($bg_img, 0, 0, $bgcolor); // 合拼图片与底图,实现描边效果
  112. 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);
  113. } /**
  114. * 对图片对象进行圆角处理
  115. * @param Obj $img 图片对象
  116. * @return Obj
  117. */
  118. private function image_fillet($img){
  119. // 获取图片宽高
  120. $img_w = imagesx($img); $img_h = imagesy($img); // 创建圆角图片对象
  121. $new_img = imagecreatetruecolor($img_w, $img_h); // 保存透明通道
  122. imagesavealpha($new_img, true); // 填充圆角图片
  123. $bg = imagecolorallocatealpha($new_img, 255, 255, 255, 127);
  124. imagefill($new_img, 0, 0, $bg); // 圆角半径
  125. $r = $this->_config['logo_radius']; // 执行圆角处理
  126. for($x=0; $x<$img_w; $x++){ for($y=0; $y<$img_h; $y++){ $rgb = imagecolorat($img, $x, $y); // 不在图片四角范围,直接画图
  127. if(($x>=$r && $x<=($img_w-$r)) || ($y>=$r && $y<=($img_h-$r))){
  128. imagesetpixel($new_img, $x, $y, $rgb); // 在图片四角范围,选择画图
  129. }else{ // 上左
  130. $ox = $r; // 圆心x坐标
  131. $oy = $r; // 圆心y坐标
  132. if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){
  133. imagesetpixel($new_img, $x, $y, $rgb);
  134. } // 上右
  135. $ox = $img_w-$r; // 圆心x坐标
  136. $oy = $r; // 圆心y坐标
  137. if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){
  138. imagesetpixel($new_img, $x, $y, $rgb);
  139. } // 下左
  140. $ox = $r; // 圆心x坐标
  141. $oy = $img_h-$r; // 圆心y坐标
  142. if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){
  143. imagesetpixel($new_img, $x, $y, $rgb);
  144. } // 下右
  145. $ox = $img_w-$r; // 圆心x坐标
  146. $oy = $img_h-$r; // 圆心y坐标
  147. if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){
  148. imagesetpixel($new_img, $x, $y, $rgb);
  149. }
  150. }
  151. }
  152. } return $new_img;
  153. } // 合拼图片并保留各自透明度
  154. private function imagecopymerge_alpha($dest_img, $src_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity){
  155. $w = imagesx($src_img); $h = imagesy($src_img); $tmp_img = imagecreatetruecolor($src_w, $src_h);
  156. imagecopy($tmp_img, $dest_img, 0, 0, $pos_x, $pos_y, $src_w, $src_h);
  157. imagecopy($tmp_img, $src_img, 0, 0, $src_x, $src_y, $src_w, $src_h);
  158. imagecopymerge($dest_img, $tmp_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity); return $dest_img;
  159. } /**
  160. * 创建目录
  161. * @param String $path
  162. * @return Boolean
  163. */
  164. private function create_dirs($path){
  165. if(!is_dir($path)){ return mkdir($path, 0777, true);
  166. } return true;
  167. } /** hex颜色转rgb颜色
  168. * @param String $color hex颜色
  169. * @return Array
  170. */
  171. private function hex2rgb($hexcolor){
  172. $color = str_replace('#', '', $hexcolor); if (strlen($color) > 3) { $rgb = array( 'r' => hexdec(substr($color, 0, 2)), 'g' => hexdec(substr($color, 2, 2)), 'b' => hexdec(substr($color, 4, 2))
  173. );
  174. } 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( 'r' => hexdec($r), 'g' => hexdec($g), 'b' => hexdec($b)
  175. );
  176. } return $rgb;
  177. } /** 获取图片类型
  178. * @param String $file 图片路径
  179. * @return int
  180. */
  181. private function get_file_ext($file){
  182. $filename = basename($file); list($name, $ext)= explode('.', $filename); $ext_type = 0; switch(strtolower($ext)){ case 'jpg': case 'jpeg': $ext_type = 2; break; case 'gif': $ext_type = 1; break; case 'png': $ext_type = 3; break;
  183. } return $ext_type;
  184. }
  185. } // class end?>

demo.php

  1. <?phprequire 'PHPQRCode.class.php';$config = array( 'ecc' => 'H', // L-smallest, M, Q, H-best
  2. 'size' => 12, // 1-50
  3. 'dest_file' => 'qrcode.png', 'quality' => 90, 'logo' => 'logo.jpg', 'logo_size' => 100, 'logo_outline_size' => 20, 'logo_outline_color' => '#FFFF00', 'logo_radius' => 15, 'logo_opacity' => 100,
  4. );// 二维码内容$data = 'http://weibo.com/fdipzone';// 创建二维码类$oPHPQRCode = new PHPQRCode();// 设定配置$oPHPQRCode->set_config($config);// 创建二维码$qrcode = $oPHPQRCode->generate($data);// 显示二维码echo '<img src="'.$qrcode.'?t='.time().'">';?>

相关推荐:

(进阶篇)php生成带logo二维码方法小结

以上就是php实现带logo二维码类的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行