dever 6 years ago
parent
commit
99be44489d
3 changed files with 58 additions and 4 deletions
  1. 8 4
      src/Lib/Img.php
  2. 25 0
      src/Store/Local.php
  3. 25 0
      src/View.php

+ 8 - 4
src/Lib/Img.php

@@ -259,7 +259,7 @@ class Img
      * @desc 对图片进行压缩处理
      * @param *
      */
-    public function compress($source, $quality = 20, $name = false)
+    public function compress($source, $quality = 75, $name = false)
     {
         $this->setSource($source);
         if (!$this->_image) {
@@ -275,13 +275,14 @@ class Img
      * @desc 对图片进行webp转换
      * @param *
      */
-    public function webp($source, $name = false)
+    public function webp($source, $quality = 75, $name = false)
     {
         $this->setSource($source);
         if (!$this->_image) {
             return $source;
         }
         if ($name) $this->setName($name);
+        $this->setQuality($quality);
         $this->loadMethod('webp');
         return $this->getDest('webp');
     }
@@ -1051,8 +1052,11 @@ class Img
     {
         $this->_check('source');
         $this->_dest['webp'] = $this->getName('.webp');
-        
-        $this->_image->setFormat('webp');
+
+        //apk add libwebp-tools
+        //Dever::run('cwebp -q 75 '.$this->_source.' -o ' . $this->_dest['webp']);
+
+        $this->_image->setImageFormat('webp');
         $this->_image->writeImage($this->_dest['webp']);
     }
 

+ 25 - 0
src/Store/Local.php

@@ -141,6 +141,31 @@ class Local extends Core implements Config
 		$this->img()->compress($source, $num, $dest);
 	}
 	
+	/**
+     * webp
+     * 
+     * @return mixed
+     */
+	public function handle_wp($num, $source = false, $dest = false)
+	{
+		if (!$source) {
+			$source = $this->output['file'];
+		}
+
+		if (!$dest) {
+			if (strpos($this->name, '_wp') !== false) {
+				$temp = explode('_wp', $this->name);
+				$this->name = $temp[0];
+			}
+			$this->ext = '.webp';
+			$this->name .= '_wp' . $num;
+			$dest = $this->file();
+		}
+
+		$this->img()->setType('im');
+		$this->img()->webp($source, $num, $dest);
+	}
+
 	/**
      * thumb
      * 

+ 25 - 0
src/View.php

@@ -12,6 +12,28 @@ use Upload\Src\Lib\Img;
 
 class View
 {
+    # webp
+    public function webp($file)
+    {
+        $host = Dever::config('host')->uploadRes;
+        $root = Dever::data() . 'upload/';
+        $source = str_replace(array('{uploadRes}', $host), $root, $file);
+        $dest = $source . '.webp';
+        if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
+            if (!is_file($dest)) {
+                Dever::run('cwebp '.$source.' -o ' . $dest . '');
+                header('Content-type: image/webp');
+                readfile($source);
+            } else {
+                header('Content-type: image/webp');
+                readfile($dest);
+            }
+        } else {
+            header('Content-type: image/webp');
+            readfile($source);
+        }
+    }
+
 	# 根据配置生成缩略图 暂时支持缩略图
 	public function get($file)
 	{
@@ -61,6 +83,9 @@ class View
         } elseif (strstr($file, '_p')) {
             $array = explode('_p', $file);
             $type = 'p';
+        } elseif (strstr($file, '_wp')) {
+            $array = explode('_wp', $file);
+            $type = 'wp';
         }
 
         if (isset($array)) {