dever 6 rokov pred
rodič
commit
bd7d68f3f5
3 zmenil súbory, kde vykonal 55 pridanie a 20 odobranie
  1. 2 0
      database/pic_water.php
  2. 51 18
      src/Lib/Img.php
  3. 2 2
      src/View.php

+ 2 - 0
database/pic_water.php

@@ -17,6 +17,8 @@ $water = array
 	3 => '右上',
 	4 => '右下',
 	5 => '居中',
+	6 => '左居中',
+	7 => '右居中',
 );
 
 return array

+ 51 - 18
src/Lib/Img.php

@@ -527,7 +527,7 @@ class Img
 
                 $water = $this->_gd_copy($water,$dest_x,$dest_y,$water_x,$water_y,0,0,false,2);
 
-                $xy = $this->_get_mark($dest_x, $dest_y, $dest_x, $dest_y);
+                $xy = $this->_get_mark($source_x, $source_y, $dest_x, $dest_y);
                 $water_x = $dest_x;
                 $water_y = $dest_y;
             } else {
@@ -745,6 +745,7 @@ class Img
         $this->_check('txt','size');
         $this->_check('txt','angle');
         $this->_check('txt','name');
+        $this->_check('txt', 'position');
         //$this->_check('txt','left');
         //$this->_check('txt','top');
         //$this->_check('txt','bgcolor');
@@ -755,14 +756,27 @@ class Img
         if ($this->_setup == true || !file_exists($this->_dest['txt'])) {
 
             $color = $this->_txt['color'];
-            $this->_txt['left'] = isset($this->_txt['left']) ? $this->_txt['left'] : 0;
-            $this->_txt['top'] = isset($this->_txt['top']) ? $this->_txt['top'] : 0;
+
+            $fontFile = isset($this->_txt['font']) ? $this->_txt['font'] : "SIMSUN.TTC";
+            $position = imagettfbbox($this->_txt['size'], $this->_txt['angle'], $fontFile, $this->_txt['name']);
+            if ($position) {
+                $source_x = imagesx($this->_image);
+                $source_y = imagesy($this->_image);
+                $water_x = $position[2] - $position[0];
+                $water_y = $position[1] - $position[7];
+
+                $xy = $this->_get_mark($source_x, $source_y, $water_x, $water_y, 'txt');
+            }
+
+            $this->_txt['left'] = isset($xy[0]) ? $xy[0] : 0;
+            $this->_txt['top'] = isset($xy[1]) ? $xy[1] : 0;
+
             if (!empty($color) && (strlen($color)==7)) { 
                 $R = hexdec(substr($color,1,2)); 
                 $G = hexdec(substr($color,3,2)); 
                 $B = hexdec(substr($color,5)); 
                 putenv('GDFONTPATH=' . realpath('.'));
-                $fontFile = isset($this->_txt['font']) ? $this->_txt['font'] : "SIMSUN.TTC";
+                
                 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']);
             }
 
@@ -1230,7 +1244,7 @@ class Img
 
                 $water->thumbnailImage($dest_x, $dest_y);
 
-                $xy = $this->_get_mark($dest_x, $dest_y, $dest_x, $dest_y);
+                $xy = $this->_get_mark($source_x, $source_y, $dest_x, $dest_y);
                 $water_x = $dest_x;
                 $water_y = $dest_y;
             } else {
@@ -1262,6 +1276,7 @@ class Img
         $this->_check('txt','size');
         $this->_check('txt','angle');
         $this->_check('txt','name');
+        $this->_check('mark', 'position');
         //$this->_check('txt','left');
         //$this->_check('txt','top');
         //$this->_check('txt','bgcolor');
@@ -1274,6 +1289,16 @@ class Img
             $draw = new \ImagickDraw();
             if (isset($this->_txt['font'])) {
                 $draw->setFont($this->_txt['font']);
+
+                $position = imagettfbbox($this->_txt['size'], $this->_txt['angle'], $this->_txt['font'], $this->_txt['name']);
+                if ($position) {
+                    $source_x   = $this->_image->getImageWidth();
+                    $source_y   = $this->_image->getImageHeight();
+                    $water_x = $position[2] - $position[0];
+                    $water_y = $position[1] - $position[7];
+
+                    $xy = $this->_get_mark($source_x, $source_y, $water_x, $water_y, 'txt');
+                }
             }
             if (isset($this->_txt['size'])) {
                 $draw->setFontSize($this->_txt['size']);
@@ -1284,12 +1309,9 @@ class Img
             if (isset($this->_txt['bgcolor'])) {
                 $draw->setTextUnderColor($this->_txt['bgcolor']);
             }
-            if (!isset($this->_txt['top'])) {
-                $this->_txt['top'] = 0;
-            }
-            if (!isset($this->_txt['left'])) {
-                $this->_txt['left'] = 0;
-            }
+
+            $this->_txt['left'] = isset($xy[0]) ? $xy[0] : 0;
+            $this->_txt['top'] = isset($xy[1]) ? $xy[1] : 0;
               
             if ($this->_imageType == 'gif') {  
                 foreach ($this->_image as $frame) {
@@ -1407,18 +1429,20 @@ class Img
         return $img;
     }
 
-    private function _get_mark($source_x, $source_y, $water_x, $water_y)
+    private function _get_mark($source_x, $source_y, $water_x, $water_y, $type = 'mark')
     {
-        $this->_check('mark', 'position');
+        $this->_check($type, 'position');
         $l = 0;
         $t = 0;
         $state = true;
 
-        if ($this->_mark['position'] && is_array($this->_mark['position'])) {
-            $l = $this->_mark['position'][0];
-            $t = $this->_mark['position'][1];
-        } elseif ($this->_mark['position']) {
-            switch ($this->_mark['position']) {
+        $method = '_' . $type;
+        $position = $this->{$method}['position'];
+        if ($position && is_array($position)) {
+            $l = $position[0];
+            $t = $position[1];
+        } elseif ($position) {
+            switch ($position) {
                 case 1:
                     //左上
                     break;
@@ -1440,6 +1464,15 @@ class Img
                     $l = $source_x/2 - $water_x/2;
                     $t = $source_y/2 - $water_y/2;
                     break;
+                case 6:
+                    //上中
+                    $l = $source_x/2 - $water_x/2;
+                    break;
+                case 7:
+                    //下中
+                    $l = $source_x/2 - $water_x/2;
+                    $t = $source_y - $water_y;
+                    break;
                 default :
                     $state = false;
                     break;

+ 2 - 2
src/View.php

@@ -126,6 +126,7 @@ class View
                 (
                     'method' => 'mark',
                     'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
+                    //left,top
                     'position' => array(100,200),
                     'width' => 100,
                     'height' => 100,
@@ -135,8 +136,7 @@ class View
                     'method' => 'txt',
                     'name' => 'test文字',
                     'color' => '#FF0000',
-                    'top' => 700,
-                    'left' => 100,
+                    'position' => array(100,200),
                     'size' => 50,
                     'angle' => 0,
                     'font' => '/www/grow/config/fonts/simsun.ttc',