dever 6 years ago
parent
commit
4da924551d
4 changed files with 109 additions and 1 deletions
  1. 11 0
      database/pic_thumb.php
  2. 65 1
      src/Lib/Img.php
  3. 30 0
      src/Store/Local.php
  4. 3 0
      src/View.php

+ 11 - 0
database/pic_thumb.php

@@ -64,6 +64,17 @@ return array
 			'update'	=> 'text',
 			'list'		=> true,
 		),
+
+		'compress'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '压缩质量-为空或者小于等于0则不进行压缩',
+			'default' 	=> '0',
+			'desc' 		=> '请输入压缩质量',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
 		
 		'state'		=> array
 		(

+ 65 - 1
src/Lib/Img.php

@@ -245,6 +245,9 @@ class Img
             $this->setSetup($setup);
         }
         $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
         if ($name) $this->setName($name);
         $this->setMark($water);
         //if($position) $this->setPosition($position);
@@ -259,12 +262,30 @@ class Img
     public function compress($source, $quality = 20, $name = false)
     {
         $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
         if ($name) $this->setName($name);
         $this->setQuality($quality);
         $this->loadMethod('compress');
         return $this->getDest('compress');
     }
 
+    /**
+     * @desc 对图片进行webp转换
+     * @param *
+     */
+    public function webp($source, $name = false)
+    {
+        $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
+        if ($name) $this->setName($name);
+        $this->loadMethod('webp');
+        return $this->getDest('webp');
+    }
+
     /**
      * @desc 构造函数 可批量建立
      * @param *
@@ -286,6 +307,9 @@ class Img
             $this->setSetup($setup);
         }
         $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
         if ($name) $this->setName($name);
         foreach ($config as $k => $v) {
             $k = $v['method'];
@@ -307,6 +331,9 @@ class Img
             $this->setSetup($setup);
         }
         $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
         if ($name) $this->setName($name);
         $this->setThumb($thumb);
         $this->loadMethod('thumb');
@@ -324,6 +351,9 @@ class Img
             $this->setSetup($setup);
         }
         $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
         if ($position) $this->setPosition($position);
         if ($name) $this->setName($name);
         $this->setCrop($crop);
@@ -343,6 +373,9 @@ class Img
         }
         
         $this->setSource($source);
+        if (!$this->_image) {
+            return $source;
+        }
         if ($position) $this->setPosition($position);
         if ($name) $this->setName($name);
         $this->setCrop($size);
@@ -425,6 +458,9 @@ class Img
     private function _image()
     {
         $this->_check('source');
+        if (!class_exists('\Imagick')) {
+            $this->_type = 'gd';
+        }
         if (!$this->_image) {
             switch ($this->_type) {
                 case 'gd' :
@@ -432,7 +468,10 @@ class Img
                     break;
                 case 'im' :
                     $this->_image = $this->_im_get($this->_source);
-                    $this->_imageType = strtolower($this->_image->getImageFormat());
+                    if ($this->_image) {
+                        $this->_imageType = strtolower($this->_image->getImageFormat());
+                    }
+                    
                     break;
             }
         }
@@ -622,6 +661,15 @@ class Img
         }
     }
 
+    /**
+     * @desc 建立原图压缩图
+     * @param *
+     */
+    private function _gd_create_compress()
+    {
+        $this->_dest['compress'] = $this->_source;
+    }
+
     /**
      * @desc 建立剪切图
      * @param *
@@ -995,6 +1043,19 @@ class Img
 		}
 	}
 
+    /**
+     * @desc 设置webp格式
+     * @param *
+     */
+    private function _im_create_webp()
+    {
+        $this->_check('source');
+        $this->_dest['webp'] = $this->getName('.webp');
+        
+        $this->_image->setFormat('webp');
+        $this->_image->writeImage($this->_dest['webp']);
+    }
+
     /**
      * @desc 建立裁切图
      * @param *
@@ -1245,6 +1306,9 @@ class Img
      */
     private function _im_get($image = false)
     {
+        if (!is_file($image)) {
+            return false;
+        }
         if ($image && strstr($image, 'http')) {
             $content = file_get_contents($image);
             $im = new \Imagick();

+ 30 - 0
src/Store/Local.php

@@ -116,6 +116,30 @@ class Local extends Core implements Config
             $this->img()->mark($this->output['file'], $water, true, $this->file());
 		}
 	}
+
+	/**
+     * compress
+     * 
+     * @return mixed
+     */
+	public function handle_p($num, $source = false, $dest = false)
+	{
+		if (!$source) {
+			$source = $this->output['file'];
+		}
+
+		if (!$dest) {
+			if (strpos($this->name, '_p') !== false) {
+				$temp = explode('_p', $this->name);
+				$this->name = $temp[0];
+			}
+			$this->name .= '_p' . $num;
+			$dest = $this->file();
+		}
+
+		$this->img()->setType('im');
+		$this->img()->compress($source, 20, $dest);
+	}
 	
 	/**
      * thumb
@@ -143,6 +167,12 @@ class Local extends Core implements Config
 			$size = $config['width'] . '_' . $config['height'] . '_2';
 
             $this->img()->thumb($source, $size, true, $dest);
+
+            # 同时进行压缩
+            if (isset($config['compress']) && $config['compress'] > 0) {
+				$this->img()->setType('im');
+				$this->img()->compress($dest, 20, $dest);
+            }
 		}
 	}
 	

+ 3 - 0
src/View.php

@@ -58,6 +58,9 @@ class View
         } elseif (strstr($file, '_t')) {
             $array = explode('_t', $file);
             $type = 't';
+        } elseif (strstr($file, '_p')) {
+            $array = explode('_p', $file);
+            $type = 'p';
         }
 
         if (isset($array)) {