dever 3 years ago
parent
commit
d93477c7ce
2 changed files with 39 additions and 9 deletions
  1. 1 1
      config/base.php
  2. 38 8
      src/Api.php

+ 1 - 1
config/base.php

@@ -10,7 +10,7 @@ $config['base'] = array
 		# 是否生成本地文件
 		'local' => false,
 		# 大小
-		'size' => 10,
+		'size' => 5,
 	)
 );
 

+ 38 - 8
src/Api.php

@@ -5,6 +5,7 @@ use Dever;
 
 class Api
 {
+    private $path = 'upload/qrcode';
     public function __construct()
     {
         //$this->config = Dever::config('base', 'project')->qrcode;
@@ -12,23 +13,52 @@ class Api
         Dever::apply('lib/phpqrcode');
     }
 
-    public function qrcode($string)
+    public function qrcode($string, $local = false, $logo = false)
     {
+        if ($local) {
+            $this->config['local'] = true;
+        }
         if ($this->config['local']) {
-            $result = $this->createFile($string);
+            $result = $this->create($string, $local, $logo);
         } else {
             $result = $this->create($string);
         }
         return $result;
     }
 
-    private function create($value)
+    private function create($value, $local = false, $logo = false)
     {
-        $QR = \QRcode::png($value, false, 'L', $this->config['size'], 2);
-    }
+        $file = false;
+        if ($local) {
+            $path = Dever::pathDay($this->path);
+            $file = $path . $local . '.png';
+        }
+        if (!is_file($file)) {
+            $QR = \QRcode::png($value, $file, 'L', $this->config['size'], 2);
 
-    private function createFile($string)
-    {
-        echo 22;die;
+            if ($logo) {
+                $logo = Dever::local($logo);
+                $QR = imagecreatefromstring(file_get_contents($file));
+                $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, $file);
+            }
+        }
+        
+        $url = str_replace(Dever::data() . 'upload/', Dever::config('host')->uploadRes, $file);
+        $result = array
+        (
+            'local' => $file,
+            'url' => $url,
+        );
+        return $result;
     }
 }