dever 7 years ago
parent
commit
40cd140a56
2 changed files with 92 additions and 43 deletions
  1. 46 15
      src/Lib/Img.php
  2. 46 28
      src/View.php

+ 46 - 15
src/Lib/Img.php

@@ -399,7 +399,7 @@ class Img
 			if ($key == false) {
 				return $this->$name;
 			} else {
-				return $this->$name[$key];
+				return $this->{$name}[$key];
 			}
         } else {
             $this->_error($name . ' error');
@@ -431,7 +431,7 @@ class Img
                     $this->_image = $this->_gd_get($this->_source);
                     break;
                 case 'im' :
-                    $this->_image = new \Imagick($this->_source);
+                    $this->_image = $this->_im_get($this->_source);
                     $this->_imageType = strtolower($this->_image->getImageFormat());
                     break;
             }
@@ -883,12 +883,12 @@ class Img
                     $dest_y = $offset[1];
                 }
 				//echo $dest_y;die;
-				$this->_image = new \Imagick($this->_source);  
+				$this->_image = $this->_im_get($this->_source);  
 				$this->_image->thumbnailImage($dest_x, $dest_y);
                 
                 if (isset($offset[2]) && $offset[2] == 3) {
 					/* 按照缩略图大小创建一个有颜色的图片 */  
-					$canvas = new \Imagick();  
+					$canvas = $this->_im_get();  
 					$color = new \ImagickPixel("white");
 					$canvas->newImage($offset[0], $offset[1], $color, 'png');
 					//$canvas->paintfloodfillimage('transparent',2000,NULL,0,0);
@@ -996,7 +996,7 @@ class Img
 			}
             
             if ($this->_dropSize) {
-				$this->_image = new \Imagick($this->_dest['crop'][$k]);
+				$this->_image = $this->_im_get($this->_dest['crop'][$k]);
 				$source_x   = $this->_image->getImageWidth();
 				$source_y   = $this->_image->getImageHeight();
 			} else {
@@ -1065,7 +1065,7 @@ class Img
 
     private function _im_gif($w, $h, $x, $y, $d = false, $num = false)
     {
-		$canvas = new \Imagick();
+		$canvas = $this->_im_get();
 
 		$canvas->setFormat("gif");
 
@@ -1076,7 +1076,7 @@ class Img
         for ($i = 0; $i < $num; $i++) {
 			$this->_image->setImageIndex($i);
 					
-			$img = new \Imagick();
+			$img = $this->_im_get();
 			$img->readImageBlob($this->_image);
 			
 			if ($d != false) {
@@ -1115,7 +1115,7 @@ class Img
             if (isset($this->_mark['radius'])) {
                 $water = $this->_im_radius($this->_mark['water'], $this->_mark['radius']);
             } else {
-                $water = new \Imagick($this->_mark['water']);
+                $water = $this->_im_get($this->_mark['water']);
             }
             $draw = new \ImagickDraw();
 
@@ -1161,30 +1161,61 @@ class Img
         if ($this->_setup == true || !file_exists($this->_dest['txt'])) {
 
             $draw = new \ImagickDraw();
-            if(isset($this->_txt['font']))          $draw->setFont($this->_txt['font']);
-            if(isset($this->_txt['size']))          $draw->setFontSize($this->_txt['size']);
-            if(isset($this->_txt['color']))         $draw->setFillColor($this->_txt['color']);
-            if(isset($this->_txt['bgcolor']))       $draw->setTextUnderColor($this->_txt['bgcolor']);
+            if (isset($this->_txt['font'])) {
+                $draw->setFont($this->_txt['font']);
+            }
+            if (isset($this->_txt['size'])) {
+                $draw->setFontSize($this->_txt['size']);
+            }
+            if (isset($this->_txt['color'])) {
+                $draw->setFillColor($this->_txt['color']);
+            }
+            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;
+            }
               
             if ($this->_imageType == 'gif') {  
                 foreach ($this->_image as $frame) {
-                    $frame->annotateImage($draw, 0, 0, $this->_txt['angle'], $this->_txt['name']);
+                    $frame->annotateImage($draw, $this->_txt['left'], $this->_txt['top'], $this->_txt['angle'], $this->_txt['name']);
                 }
             } else {
-                $this->_image->annotateImage($draw, 0, 0, $this->_txt['angle'], $this->_txt['name']);
+                $this->_image->annotateImage($draw, $this->_txt['left'], $this->_txt['top'], $this->_txt['angle'], $this->_txt['name']);
             }
 
             $this->_image->writeImage($this->_dest['txt']);
         }
     }
 
+    /**
+     * @desc 获取数据源
+     * @param *
+     */
+    private function _im_get($image = false)
+    {
+        if ($image && strstr($image, 'http')) {
+            $content = file_get_contents($image);
+            $im = new \Imagick();
+            $im->readImageBlob($content);
+        } else {
+            $im = new \Imagick($image);
+        }
+        
+        return $im;
+    }
+
     /**
      * @desc 圆角图片
      * @param *
      */
     private function _im_radius($img = '', $radius = -1)
     {
-        $image = new \Imagick($img);
+        $image = $this->_im_get($img);
         $image->setImageFormat('png');
         if ($radius == -1) {
             $x = $image->getImageWidth() / 2;

+ 46 - 28
src/View.php

@@ -70,37 +70,55 @@ class View
      *
      * @return mixed
      */
-	public function create($config = array(), $filename = '')
+	public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
 	{
 		$this->img = isset($this->img) ? $this->img : new Img();
 
-		$mark = array
-		(
-			'method' => 'mark',
-			'water' => $config['image'][0]['url'],
-			'position' => array(100,200),
-			'width' => 100,
-			'height' => 100,
-			# 开启圆角
-			//'radius' => -1,
-		);
-
-		$send[] = $mark;
-
-		$mark = array
-		(
-			'method' => 'mark',
-			'water' => $config['image'][0]['url'],
-			'position' => array(200,500),
-			'width' => 100,
-			'height' => 100,
-			# 开启圆角
-			//'radius' => -1,
-		);
-		$send[] = $mark;
-
-		$name = $this->img->init($config['background'], $send, true, $config['background'] . '.jpg');
-		print_r($name);die;
+        /*
+        $config = array
+        (
+            'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',
+            'param' => array
+            (
+                0 => array
+                (
+                    'method' => 'mark',
+                    'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
+                    'position' => array(100,200),
+                    'width' => 100,
+                    'height' => 100,
+                ),
+                1 => array
+                (
+                    'method' => 'txt',
+                    'name' => 'test文字',
+                    'color' => '#FF0000',
+                    'top' => 700,
+                    'left' => 100,
+                    'size' => 50,
+                    'angle' => 0,
+                    'font' => '/www/grow/config/fonts/simsun.ttc',
+                ),
+            ),
+        );
+        */
+
+        if (!$filename) {
+            $filename = Dever::id();
+        }
+        $file =  Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';
+
+        if (!is_file($file) || $update == 1) {
+            $this->img->setType($type);
+            $result = $this->img->init($config['background'], $config['param'], true, $file);
+            if ($result) {
+                $file = array_pop($result);
+            }
+        } else {
+
+        }
+        $file = Dever::pic($file);
+        return $file;
 	}
 
 	public function creates($config = array(), $filename = '')