dever 6 years ago
parent
commit
90b4a83f16
9 changed files with 194 additions and 136 deletions
  1. 1 1
      assets/html/manage.html
  2. 1 0
      database/cate.php
  3. 1 0
      lib/Manage.php
  4. 121 26
      lib/Store/Core.php
  5. 11 0
      lib/Store/Handle.php
  6. 18 48
      lib/Store/Local.php
  7. 23 46
      lib/Store/Qiniu.php
  8. 12 14
      src/Save.php
  9. 6 1
      src/View.php

+ 1 - 1
assets/html/manage.html

@@ -347,7 +347,7 @@
   <script src="<{Dever::assets('lib/jquery/jquery.min.js', 'script')}>"></script>
   <script src="<{Dever::assets('lib/bootstrap/js/bootstrap.min.js', 'script')}>"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
-  <script src="<{Dever::assets('lib/cropper/cropper.min.js', 'script')}>"></script>
+  <script src="<{Dever::assets('lib/cropper/cropper.js', 'script')}>"></script>
   <script src="<{Dever::assets('lib/cropper/main.js', 'script')}>"></script>
 </body>
 </html>

+ 1 - 0
database/cate.php

@@ -71,6 +71,7 @@ return array
         'value' => array
         (
             '"默认分类",1,' . time(),
+            '"裁剪图片",1,' . time(),
         ),
     ),
 );

+ 1 - 0
lib/Manage.php

@@ -77,6 +77,7 @@ class Manage
 
         $table['存储位置'] = $location;
         $table['文件名'] = $info['name'];
+        $table['文件大小'] = $info['width'] . '*' . $info['height'];
         $html .= Dever::table($table);
         return $html;
     }

+ 121 - 26
lib/Store/Core.php

@@ -30,6 +30,10 @@ class Core
 	protected $size;
 
 	protected $base = '';
+
+	protected $base64 = false;
+
+	protected $yun = false;
 	
 	/**
 	 * __construct
@@ -147,7 +151,7 @@ class Core
 	 */
 	private function check_size()
 	{
-		if ($this->config['width'] > 0 || $this->config['height'] > 0) {
+		if (!$this->limit) {
 			$this->limit = getimagesize($this->data['file']['tmp_name']);
 		}
 
@@ -178,38 +182,82 @@ class Core
 		$this->output['status'] = 1; 
 
 		if (is_string($this->data['file'])) {
-			/*
-			if (strpos($this->data['file'], 'http') !== false) {
-				$this->output['status'] = -1;
-				$this->output['message'] = '暂时不支持复制网络文件';
-				return $this->output;
+			if (strstr($this->data['file'], 'base64,') && isset($this->data['pic'])) {
+				$temp = explode('base64,', $this->data['file']);
+				$type = str_replace(array('data:', ';'), '', $temp[0]);
+				$file = str_replace(' ', '+', $temp[1]);
+				$file = str_replace('=', '', $temp[1]);
+		        $file = base64_decode($file);
+		        $name = $this->data['local'] = Dever::local($this->data['pic']);
+		        $size = strlen($file);
+		        $size = ($size - ($size/8)*2)/1024;
+		        $this->base64 = true;
+			} else {
+				$name = urldecode($this->data['file']);
 			}
-			*/
-			$this->root();
 
-			header('Content-type: text/json; charset=utf-8');
-
-			$path = Dever::path($this->base, 'tmp/');
-			$name = urldecode($this->data['file']);
 			$this->data['file'] = array();
-			$this->data['file']['name'] = 'Tmp' . sha1($name);
-			//$this->data['file']['name'] = 'Tmp' . Helper::rand(8) . md5(microtime() . rand(0,1000)) . '.jpg';
-			$this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
-
-			if (!is_file($this->data['file']['tmp_name'])) {
-				if (strstr($name, 'tp=webp')) {
-					$name = str_replace('tp=webp', 'tp=jpeg', $name);
-				} elseif (strstr($name, '.webp')) {
-					$name = str_replace('.webp', '.jpg', $name);
+			if (!$this->yun && strstr($name, 'http')) {
+				$this->base64 = false;
+				$this->root();
+				header('Content-type: text/json; charset=utf-8');
+				$path = Dever::path($this->base, 'tmp/');
+				
+				$this->data['file']['name'] = 'Tmp' . sha1($name);
+				$this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
+
+				if (!is_file($this->data['file']['tmp_name'])) {
+					if (strstr($name, 'tp=webp')) {
+						$name = str_replace('tp=webp', 'tp=jpeg', $name);
+					} elseif (strstr($name, '.webp')) {
+						$name = str_replace('.webp', '.jpg', $name);
+					}
+					if (!isset($file)) {
+						$file = Dever::curl($name);
+					}
+					
+					if (stristr($file, 'webp')) {
+						# 将webp图片转成jpg
+						$this->ext = '.jpg';
+					}
+					file_put_contents($this->data['file']['tmp_name'], $file);
+					$this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
 				}
-				$file = Dever::curl($name);
-				if (stristr($file, 'webp')) {
-					# 将webp图片转成jpg
+			} else {
+				$this->data['file']['name'] = $name;
+				$this->data['file']['tmp_name'] = $file;
+			}
+
+			if (isset($type)) {
+				$this->data['file']['type'] = $type;
+			}
+
+			if (isset($size)) {
+				$this->data['file']['size'] = $size;
+			}
+
+			if (isset($this->data['param'])) {
+				$this->limit = array($this->data['param']['param_w'], $this->data['param']['param_h']);
+				if (isset($this->data['name'])) {
+					$param = implode('_', array_values($this->data['param']));
+					$this->data['file']['name'] = '.' . $this->data['name'] . $param;
+					if ($this->base64) {
+						$this->data['name'] = $this->data['file']['name'];
+					} else {
+						$filename = md5($this->data['pic']) . '.' . pathinfo($this->data['pic'], PATHINFO_EXTENSION);
+						$file = $this->base . $this->data['key'] . $this->path . $filename;
+						if (!is_file($file)) {
+							$content = Dever::curl($name);
+							$this->path();
+							file_put_contents($file, $content);
+						}
+						
+						$this->data['name'] = $filename . $this->data['file']['name'];
+					}
+					
 					$this->ext = '.jpg';
 				}
-				file_put_contents($this->data['file']['tmp_name'], $file);
 			}
-			$this->data['file']['size'] = filesize($this->data['file']['tmp_name']);
 			
 		} else {
 			header("Content-type: application/json; charset=utf-8");
@@ -262,6 +310,10 @@ class Core
 		$param['set_key'] = md5($this->output['url']);
 		$param['set_ext'] = $this->ext;
 
+		if (isset($this->data['cate'])) {
+			$param['set_cate'] = $this->data['cate'];
+		}
+
 		if ($this->limit) {
 			$param['set_width'] = $this->limit[0];
 			$param['set_height'] = $this->limit[1];
@@ -284,6 +336,9 @@ class Core
 		$param['add_key'] = md5($this->output['url']);
 		$param['add_ext'] = $this->ext;
 		$param['add_upload'] = $this->data['key'];
+		if (isset($this->data['cate'])) {
+			$param['add_cate'] = $this->data['cate'];
+		}
 
 		if ($this->limit) {
 			$param['add_width'] = $this->limit[0];
@@ -302,6 +357,46 @@ class Core
 		Dever::load('upload/file-insert', $param);
 	}
 
+	private function img()
+	{
+		$this->img = isset($this->img) ? $this->img : new Img();
+		$this->img->setType('im');
+
+		return $this->img;
+	}
+
+	protected function getName()
+    {
+        $this->path()->name($this->data['file']['name'])->ext()->file();
+    }
+
+	protected function path()
+	{
+		$date = explode('-', date("Y-m-d"));
+
+		$this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
+
+		return $this;
+	}
+
+	protected function name($name)
+	{
+		if (isset($this->data['name'])) {
+			$this->name = $this->data['name'];
+		} else {
+			$this->name = md5($name);
+		}
+
+		return $this;
+	}
+
+	protected function ext()
+	{
+		$this->ext  = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
+
+		return $this;
+	}
+
 	/**
 	 * getExt
 	 * 

+ 11 - 0
lib/Store/Handle.php

@@ -43,6 +43,17 @@ class Handle
         return $this->get('getName');
     }
 
+    # 手动裁图
+    public function cropper()
+    {
+        if ($this->local) {
+            $this->local->cropper();
+        }
+        if ($this->yun) {
+            $this->yun->cropper();
+        }
+    }
+
 
     # 获取文件名
     public function get($type = 'copy')

+ 18 - 48
lib/Store/Local.php

@@ -7,6 +7,7 @@ use Dever\Support\Img;
 
 class Local extends Core implements Config
 {
+	public $yun = false;
 	/**
      * 创建文件和目录
      * 
@@ -16,7 +17,7 @@ class Local extends Core implements Config
 	{
 		$this->root();
 		
-		$this->path()->name($this->data['file']['name'])->ext()->file();
+		$this->getName();
 		
 		# 不覆盖
 		if (is_file($this->output['file']) && $this->config['cover'] == 2) {
@@ -32,7 +33,7 @@ class Local extends Core implements Config
 			}
 
 			if (!$this->limit) {
-				//$this->limit = getimagesize($this->output['file']);
+				$this->limit = getimagesize($this->output['file']);
 			}
 
 			if (!$this->size) {
@@ -45,8 +46,10 @@ class Local extends Core implements Config
 			} else {
 				$this->insert();
 			}
-			
-			if (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
+
+			if ($this->base64) {
+				file_put_contents($this->output['file'], $this->data['file']['tmp_name']);
+			} elseif (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
 				$this->output['status'] = -1;
 				$this->output['message'] = '文件已经存在';
 			} else {
@@ -54,60 +57,27 @@ class Local extends Core implements Config
 			}
 
 			@chmod($this->output['file'], 0755);
-            @system('chmod -R 777 ' . $this->output['file']);
+            //@system('chmod -R 777 ' . $this->output['file']);
 
 			return $this->output['url'];
 		}
 	}
 
-	public function getName()
-    {
-    	print_r($this->data['file']['name']);die;
-        $this->path()->name($this->data['file']['name'])->ext()->file();
-        return $this->file;
-    }
-
-	private function path()
-	{
-		$date = explode('-', date("Y-m-d"));
-
-		$this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
-
-		return $this;
-	}
-
-	private function name($name)
-	{
-		$this->name = md5($name);
-
-		return $this;
-	}
-
-	private function ext()
+	public function file()
 	{
-		$this->ext  = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
-
-		return $this;
-	}
-
-	private function file()
-	{
-		$this->file = $this->path . $this->name . $this->ext;
-
-		$this->output['file'] = is_file($this->base . $this->file) ? $this->base . $this->file : Dever::path($this->base, $this->file);
-		$this->output['url'] = Dever::config('host')->uploadRes . $this->file;
+		if ($this->base64 && isset($this->data['local'])) {
+			$this->file = $this->data['local'] . $this->name . $this->ext;
+			$this->output['file'] = $this->file;
+			$this->output['url'] = Dever::pic($this->output['file']);
+		} else {
+			$this->file = $this->path . $this->name . $this->ext;
+			$this->output['file'] = is_file($this->base . $this->file) ? $this->base . $this->file : Dever::path($this->base, $this->file);
+			$this->output['url'] = Dever::config('host')->uploadRes . $this->file;
+		}
 
 		return $this->output['file'];
 	}
 
-	private function img()
-	{
-		$this->img = isset($this->img) ? $this->img : new Img();
-		$this->img->setType('im');
-
-		return $this->img;
-	}
-	
 	/**
      * water
      * 

+ 23 - 46
lib/Store/Qiniu.php

@@ -6,6 +6,7 @@ Dever::apply('sdk/qiniu', 'upload');
 
 class Qiniu extends Core implements Config
 {
+    public $yun = true;
     /**
      * 创建文件和目录
      * 
@@ -30,14 +31,19 @@ class Qiniu extends Core implements Config
         // 构建 UploadManager 对象
         $upload = new \Qiniu\Storage\UploadManager();
 
-        $this->path()->name($this->data['file']['name'])->ext()->file();
+        $this->getName();
 
         $file = $this->file;
         if ($this->config['cover'] == 3) {
             $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
             $file = $this->file;
         }
-        list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
+
+        if ($this->base64) {
+            list($ret, $err) = $upload->put($token, $file, $this->data['file']['tmp_name'], $param = null);
+        } else {
+            list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
+        }
 
         if (isset($ret['hash']) && isset($ret['key'])) {
 
@@ -45,11 +51,14 @@ class Qiniu extends Core implements Config
             $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
             
             if (!$this->limit) {
-                //$this->limit = getimagesize($this->output['file']);
-            }
 
-            if (!$this->size && isset($this->data['file']['size'])) {
-                $this->size = $this->data['file']['size'];
+                $data = Dever::curl($this->output['url'] . '?imageInfo');
+                if ($data) {
+                    $data = json_decode($data, true);
+                    $this->limit = array($data['width'], $data['height']);
+                    $this->size = $data['size'];
+                    $this->ext = '.' . $data['format'];
+                }
             }
             
             if (isset($file) && $file) {
@@ -63,52 +72,20 @@ class Qiniu extends Core implements Config
         }
     }
 
-    public function getName()
-    {
-        $this->path()->name($this->data['file']['name'])->ext()->file();
-        return $this->file;
-    }
-
-    private function path()
-    {
-        $date = explode('-', date("Y-m-d"));
-
-        $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
-
-        return $this;
-    }
-
-    private function name($name)
-    {
-        $this->name = md5($name);
-
-        return $this;
-    }
-
-    private function ext()
-    {
-        $this->ext  = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
-
-        return $this;
-    }
-
-    private function file()
+    public function file()
     {
-        $this->file = $this->path . $this->name . $this->ext;
+        if ($this->base64) {
+            $data = pathinfo($this->data['pic']);
+            $data['dirname'] = str_replace($this->host, '', $data['dirname']);
+            $this->file = $data['dirname'] . '/' . $data['basename'] . $this->name . $this->ext;
+        } else {
+            $this->file = $this->path . $this->name . $this->ext;
+        }
 
         $this->output['file'] = $this->file;
         $this->output['url'] = $this->host . $this->file;
-
         return $this->output['file'];
     }
-
-    private function img()
-    {
-        $this->img = isset($this->img) ? $this->img : new Img();
-        $this->img->setType('im');
-
-        return $this->img;
-    }
     
     /**
      * water

+ 12 - 14
src/Save.php

@@ -18,20 +18,18 @@ class Save
 
 	public function cropper()
 	{
-		$pic = Dever::input('pic');
-		$key = Dever::input('key');
-		$local = Dever::local($pic);
-		$img = Dever::input('img');
-		$temp = explode('base64,', $img);
-		$img = str_replace(' ', '+', $temp[1]);
-        $img = base64_decode($img);
-
-        $param = Dever::preInput('param');
-        $name = implode('_', array_values($param));
-
-		$local .= '.cr_' . $name . '.jpg';
-		file_put_contents($local, $img);
-		return Dever::pic($local);
+        $send['param'] = Dever::preInput('param');
+		$send['key'] = Dever::input('key');
+		$send['pic'] = Dever::input('pic');
+		$send['cate'] = 2;
+		$send['name'] = 'cr_';
+		$send['file'] = Dever::input('img');
+		$handle = new Handle($send);
+
+		$this->output = $handle->copy();
+		unset($this->output['file']);
+		
+		Dever::outDiy($this->output);
 	}
 
 	private function upload($file = false, $key = 1)

+ 6 - 1
src/View.php

@@ -36,6 +36,11 @@ class View
         $data['search_cate'] = 1;
         $data['cate'] = Dever::db('upload/cate')->state();
         $data['param'] = '';
+        if (strstr($data['pic'], '.cr_')) {
+            $temp = explode('.cr_', $data['pic']);
+            $param = $data['pic'];
+            $data['pic'] = $temp[0];
+        }
         if (strstr($param, '.cr_')) {
             $temp = explode('.cr_', $param);
             $data['param'] = str_replace('.jpg', '', $temp[1]);
@@ -50,7 +55,7 @@ class View
         $data = array();
 
         $key = Dever::input('key');
-        $cate = Dever::input('cate');
+        $cate = Dever::input('cate', 1);
         $name = Dever::input('name');
         $tag = Dever::input('tag');
         $data['cur'] = Dever::input('cur');