QRcode.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace dh2y\qrcode;
  3. use think\Exception;
  4. use think\facade\Config;
  5. use think\facade\Request;
  6. class QRcode
  7. {
  8. protected $config = []; //相关配置
  9. protected $cache_dir = ''; //二维码缓存
  10. protected $outfile = ''; //输出二维码文件
  11. protected $error = ''; //错误信息
  12. public function __construct(){
  13. $this->config= Config::get('qrcode.');
  14. if(isset($this->config['cache_dir'])&&$this->config['cache_dir']!=''){
  15. $this->cache_dir = $this->config['cache_dir'];
  16. }else{
  17. $this->cache_dir = 'uploads/qrcode';
  18. }
  19. if (!file_exists($this->cache_dir)){
  20. mkdir ($this->cache_dir,0775,true);
  21. }
  22. require("phpqrcode/qrlib.php");
  23. }
  24. /**
  25. * 生成普通二维码
  26. * @param string $url 生成url地址
  27. * @param bool $outfile 生成文件路路径
  28. * @param int $size
  29. * @param string $evel
  30. * @return $this
  31. */
  32. public function png($url, $outfile, $size=5, $evel='H'){
  33. if($outfile == '') $outfile = $this->cache_dir.'/'.time().'.png';
  34. $this->outfile = $outfile;
  35. \QRcode::png($url,$outfile,$evel,$size,2);
  36. return $this;
  37. }
  38. /**
  39. * 显示二维码
  40. */
  41. public function show(){
  42. $url = Request::instance()->domain().'/'.$this->outfile;
  43. exit('<img src="'.$url.'"/>');
  44. }
  45. /**
  46. * 返回url路径
  47. * @return string
  48. */
  49. public function entry(){
  50. return Request::instance()->domain().'/'.$this->outfile;
  51. }
  52. /**
  53. * 返回生成二维码的相对路径
  54. * @param bool $ds
  55. * @return string
  56. */
  57. public function getPath($ds=true){
  58. if($ds){
  59. return '/'.$this->outfile;
  60. }else{
  61. return $this->outfile;
  62. }
  63. }
  64. /**
  65. * 销毁内容
  66. */
  67. public function destroy(){
  68. @unlink($this->outfile);
  69. }
  70. /**
  71. * 添加logo到二维码中
  72. * @param $logo
  73. * @return bool|mixed
  74. */
  75. public function logo($logo){
  76. if (!isset($logo)||$logo=='') {
  77. $this->error = 'logo不存在';
  78. return false;
  79. }
  80. $QR = imagecreatefromstring(file_get_contents($this->outfile));
  81. $logo = imagecreatefromstring(file_get_contents($logo));
  82. $QR_width = imagesx($QR);//二维码图片宽度
  83. $QR_height = imagesy($QR);//二维码图片高度
  84. $logo_width = imagesx($logo);//logo图片宽度
  85. $logo_height = imagesy($logo);//logo图片高度
  86. $logo_qr_width = $QR_width / 5;
  87. $scale = $logo_width/$logo_qr_width;
  88. $logo_qr_height = $logo_height/$scale;
  89. $from_width = ($QR_width - $logo_qr_width) / 2;
  90. //重新组合图片并调整大小
  91. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  92. $this->outfile = $this->cache_dir.'/'.time().'.png';
  93. imagepng($QR, $this->outfile);
  94. imagedestroy($QR);
  95. return $this;
  96. }
  97. /**
  98. * 添加背景图
  99. * @param int $x 二维码在背景图X轴未知
  100. * @param int $y 二维码在背景图Y轴未知
  101. * @param string $dst_path
  102. * @return $this
  103. */
  104. public function background($x=200,$y=500,$dst_path = ''){
  105. if($dst_path==''){
  106. $dst_path = $this->config['background'];
  107. }
  108. $src_path = $this->outfile;//覆盖图
  109. //创建图片的实例
  110. $dst = imagecreatefromstring(file_get_contents($dst_path));
  111. $src = imagecreatefromstring(file_get_contents($src_path));
  112. //获取覆盖图图片的宽高
  113. list($src_w, $src_h) = getimagesize($src_path);
  114. //将覆盖图复制到目标图片上,最后个参数100是设置透明度(100是不透明),这里实现不透明效果
  115. imagecopymerge($dst, $src, $x, $y, 0, 0, $src_w, $src_h, 100);
  116. $this->outfile = $this->cache_dir.'/'.time().'.png';
  117. imagepng($dst, $this->outfile);//根据需要生成相应的图片
  118. imagedestroy($dst);
  119. return $this;
  120. }
  121. public function text($text,$size,$locate =[],$color = '#00000000',$font='simsun.ttc', $offset = 0, $angle = 0) {
  122. $dst_path = $this->outfile;
  123. //创建图片的实例
  124. $dst = imagecreatefromstring(file_get_contents($dst_path));
  125. /* 设置颜色 */
  126. if (is_string($color) && 0 === strpos($color, '#')) {
  127. $color = str_split(substr($color, 1), 2);
  128. $color = array_map('hexdec', $color);
  129. if (empty($color[3]) || $color[3] > 127) {
  130. $color[3] = 0;
  131. }
  132. } elseif (!is_array($color)) {
  133. throw new Exception('错误的颜色值');
  134. }
  135. //如果字体不存在 用composer项目自己的字体
  136. if(!is_file($font)){
  137. $font = dirname(__FILE__).'/'.$font;
  138. }
  139. //获取文字信息
  140. $info = imagettfbbox($size, $angle, $font, $text);
  141. $minx = min($info[0], $info[2], $info[4], $info[6]);
  142. $maxx = max($info[0], $info[2], $info[4], $info[6]);
  143. $miny = min($info[1], $info[3], $info[5], $info[7]);
  144. $maxy = max($info[1], $info[3], $info[5], $info[7]);
  145. /* 计算文字初始坐标和尺寸 */
  146. $x = $minx;
  147. $y = abs($miny);
  148. $w = $maxx - $minx;
  149. $h = $maxy - $miny;
  150. //背景图信息
  151. list($dst_w, $dst_h) = getimagesize($dst_path);
  152. if (is_array($locate)) {
  153. list($posx, $posy) = $locate;
  154. $x += ($posx=='center')?(($dst_w - $w) / 2):$posx;
  155. $y += ($posy=='center')?(($dst_h - $h) / 2):$posy;
  156. } else {
  157. throw new Exception('不支持的文字位置类型');
  158. }
  159. //字体颜色
  160. $black = imagecolorallocate($dst, $color[0], $color[1], $color[2]);
  161. //加入文字
  162. imagefttext($dst, $size, $angle, $x, $y, $black,$font, $text);
  163. //生成图片
  164. $this->outfile = $this->cache_dir.'/'.time().'.png';
  165. imagepng($dst, $this->outfile);
  166. imagedestroy($dst);
  167. return $this;
  168. }
  169. }