dever 5 years ago
parent
commit
936adbec9e
2 changed files with 37 additions and 1 deletions
  1. 33 1
      src/Lib/Img.php
  2. 4 0
      src/Qiniu.php

+ 33 - 1
src/Lib/Img.php

@@ -758,6 +758,12 @@ class Img
             $color = $this->_txt['color'];
 
             $fontFile = isset($this->_txt['font']) ? $this->_txt['font'] : "SIMSUN.TTC";
+
+            $this->_txt['autowrap'] = 0;
+            if (isset($this->_txt['width']) && $this->_txt['width'] > 0) {
+                $this->_txt['name'] = $this->_gd_autowrap($this->_txt['size'], $this->_txt['angle'], $fontFile, $this->_txt['name'], $this->_txt['width']);
+            }
+            
             $position = imagettfbbox($this->_txt['size'], $this->_txt['angle'], $fontFile, $this->_txt['name']);
             if ($position) {
                 $source_x = imagesx($this->_image);
@@ -777,13 +783,39 @@ class Img
                 $B = hexdec(substr($color,5)); 
                 putenv('GDFONTPATH=' . realpath('.'));
                 
-                imagettftext($this->_image, $this->_txt['size'],$this->_txt['angle'], $this->_txt['left'], $this->_txt['top'], imagecolorallocate($this->_image, $R, $G, $B),$fontFile,$this->_txt['name']);
+                imagettftext($this->_image, $this->_txt['size'],$this->_txt['angle'], $this->_txt['left'], $this->_txt['top'] + $this->_txt['autowrap'], imagecolorallocate($this->_image, $R, $G, $B),$fontFile,$this->_txt['name']);
             }
 
             imagejpeg($this->_image, $this->_dest['txt']);
         }
     }
 
+    /**
+     * @desc 文字自动换行
+     * @param *
+     */
+    private function _gd_autowrap($fontsize, $angle, $fontface, $string, $width) {
+        // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
+        $content = "";
+
+        // 将字符串拆分成一个个单字 保存到数组 letter 中
+        for ($i=0;$i<mb_strlen($string);$i++) {
+            $letter[] = mb_substr($string, $i, 1);
+        }
+
+        foreach ($letter as $l) {
+            $teststr = $content." ".$l;
+            $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
+            // 判断拼接后的字符串是否超过预设的宽度
+            if (($testbox[2] > $width) && ($content !== "")) {
+                $content .= "\n";
+                $this->_txt['autowrap'] += $this->_txt['size'];
+            }
+            $content .= $l;
+        }
+        return $content;
+    }
+
     /**
      * @desc 销毁资源
      * @param *

+ 4 - 0
src/Qiniu.php

@@ -81,6 +81,7 @@ class Qiniu
 
     public function token()
     {
+        $result = Dever::input('result', 2);
         $key = Dever::input('key');
         $upload = Dever::db('upload/upload')->one($key);
         if (!$upload) {
@@ -111,6 +112,9 @@ class Qiniu
         $domain = 'http://up-z1.qiniup.com/';
         $ret = array('uptoken' => $token, 'domain' => $domain, 'host' => $config['host']);
 
+        if ($result == 1) {
+            return $ret;
+        }
         echo json_encode($ret);die;
     }
 }