dever 4 years ago
parent
commit
19230af0c7
11 changed files with 139 additions and 42 deletions
  1. 3 4
      assets/html/cover.html
  2. 11 1
      database/file.php
  3. 8 6
      database/upload.php
  4. 11 6
      lib/Store/Core.php
  5. 21 3
      lib/Store/Oss.php
  6. 13 3
      lib/Store/Qiniu.php
  7. 11 4
      lib/View/Oss.php
  8. 15 7
      lib/View/Qiniu.php
  9. 5 1
      src/Save.php
  10. 39 5
      src/View.php
  11. 2 2
      src/Yun.php

+ 3 - 4
assets/html/cover.html

@@ -33,7 +33,7 @@
     <input type="hidden" id="cover_key" value="<{$key}>">
     <input type="hidden" id="cover_cur" value="<{$cur}>">
     <input type="hidden" id="cover_pic_url" value="<{$cover_pic_url}>">
-    <div class="layui-inline">第几秒截图:<div class="layui-input-inline"><input type="text" style="width: 100%;" class="layui-input" id="cover_num" value="1" ></div></div>
+    <div class="layui-inline">第几秒截图:<div class="layui-input-inline"><input type="text" style="width: 100%;" class="layui-input" id="cover_num" value="<{$num}>" ></div></div>
 
     <div class="layui-inline" class="file-search" onclick="crop()"><div class="layui-input-inline"><button type="button" class="layui-btn"><i class="fa fa-save"></i> 生成</button></div></div>
 
@@ -43,12 +43,12 @@
     <div class="layui-row layui-col-space15">
       <div class="layui-col-md6">
         <div class="layui-panel">
-          <iframe src="<{$video}>" scrolling="no" style="border: medium none;overflow: hidden;" height="200"></iframe>
+          <iframe src="<{$video}>" scrolling="no" style="border: medium none;overflow: hidden;" width="300"></iframe>
         </div>   
       </div>
       <div class="layui-col-md6">
         <div class="layui-panel">
-          <img src="<{$cover_pic}>" id="cover_pic" height="200" />
+          <img src="<{$cover_pic}>" id="cover_pic" width="300" />
         </div>   
       </div>
     </div>    
@@ -65,7 +65,6 @@ function crop()
         $('#cover_pic').attr('src', t.data);
     })
 }
-crop();
 </script>
 
 </div>

+ 11 - 1
database/file.php

@@ -195,12 +195,22 @@ return array
 		'pic'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '封面图',
+			'name' 		=> '封面图-一般为视频截图',
 			'default' 	=> '',
 			'desc' 		=> '请输入封面图',
 			'match' 	=> 'is_string',
 			//'list'		=> true,
 		),
+
+		'pic_num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '视频截图的秒数',
+			'default' 	=> '',
+			'desc' 		=> '视频截图的秒数',
+			'match' 	=> 'is_numeric',
+			//'list'		=> true,
+		),
 		
 		'width'		=> array
 		(

+ 8 - 6
database/upload.php

@@ -228,14 +228,16 @@ return array
 	# 默认值
 	'default' => array
 	(
-		'col' => 'name,cover,type,state,cdate',
+		'col' => 'name,cover,type,size,state,cdate',
 		'value' => array
 		(
-			'"默认图片配置",1,"jpg,png,gif",1,' . time(),
-			'"默认音频配置",1,"mp3",1,' . time(),
-			'"默认视频配置",1,"video",1,' . time(),
-			'"默认文件配置",1,"jpg,png,gif,doc,pdf,rar,zip,xls,xlsx,docx,msi",1,' . time(),
-			'"默认音视频配置",1,"video,mp3,flv,mp4",1,' . time(),
+			'"默认图片配置",1,"jpg,png,gif,webp",2,1,' . time(),
+			'"默认音频配置",1,"mp3,m4a",20,1,' . time(),
+			'"默认视频配置",1,"video,flv,mp4,webm",200,1,' . time(),
+			'"默认文件配置",1,"jpg,png,gif,doc,pdf,rar,zip,xls,xlsx,docx,msi",20,1,' . time(),
+			'"默认音视频配置",1,"video,mp3,flv,mp4,webm,mov,m4a",200,1,' . time(),
+			'"用户上传图片",1,"jpg,png,gif,webp",2,1,' . time(),
+			'"裁剪图片",1,"jpg,png,gif,webp",2,1,' . time(),
 		),
 	),
 

+ 11 - 6
lib/Store/Core.php

@@ -225,13 +225,18 @@ class Core
 				header('Content-type: text/json; charset=utf-8');
 				$path = Dever::path($this->base, 'tmp/');
 				
-				$ext = $name;
-				if (strstr($name, '?')) {
-					$temp = explode('?', $ext);
-					$ext = $temp[0];
+				$filename = $name;
+				if (strstr($filename, '?')) {
+					$temp = explode('?', $filename);
+					$filename = $temp[0];
 				}
-				$this->ext = '.' . pathinfo($ext, PATHINFO_EXTENSION);
-				$this->data['file']['name'] = 'Tmp' . sha1($name);
+				if (isset($this->data['ext']) && $this->data['ext']) {
+					$this->ext = '.' . $this->data['ext'];
+				} else {
+					$this->ext = '.' . pathinfo($filename, PATHINFO_EXTENSION);
+				}
+				
+				$this->data['file']['name'] = 'Tmp' . sha1($filename);
 				$this->data['file']['tmp_name'] = $path . $this->data['file']['name'];
 
 				if (!is_file($this->data['file']['tmp_name'])) {

+ 21 - 3
lib/Store/Oss.php

@@ -16,7 +16,7 @@ class Oss extends Core implements Config
 
         $options = array();
 
-        $ret = Dever::load('upload/lib/view/oss')->connect($yun, $this->config)->upload($this->config['bucket'], $file, $this->data['file']['tmp_name'], $options, $this->base64);
+        $ret = Dever::load('upload/lib/view/oss')->connect($yun, $this->config, $file)->upload($this->config['bucket'], $file, $this->data['file']['tmp_name'], $options, $this->base64);
 
         if (isset($ret['info']['url'])) {
 
@@ -159,7 +159,16 @@ class Oss extends Core implements Config
             //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
             //?x-oss-process=image/resize,q_50
            // $dest = $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
-            $dest = $this->data['host'] . $source . '?x-oss-process=image/resize,m_lfit,w_'.$config['width'].',h_' . $config['height'];
+            if (!strstr($source, 'http')) {
+                $source = $this->data['host'] . $source;
+            }
+            if (strstr($source, '?')) {
+                $prefix = '&';
+            } else {
+                $prefix = '?';
+            }
+
+            $dest = $source . $prefix . '?x-oss-process=image/resize,m_lfit,w_'.$config['width'].',h_' . $config['height'];
 
             if (isset($config['compress']) && $config['compress'] > 0) {
                 $dest .= ',q_' . $config['compress'];
@@ -185,7 +194,16 @@ class Oss extends Core implements Config
                 $source = $this->output['file'];
             }
 
-            $dest = $this->data['host'] . $source . '?x-oss-process=image/resize,fill,w_'.$config['width'].',h_' . $config['height'];
+            if (!strstr($source, 'http')) {
+                $source = $this->data['host'] . $source;
+            }
+            if (strstr($source, '?')) {
+                $prefix = '&';
+            } else {
+                $prefix = '?';
+            }
+
+            $dest = $source . $prefix . '?x-oss-process=image/resize,fill,w_'.$config['width'].',h_' . $config['height'];
         }
         return $dest;
     }

+ 13 - 3
lib/Store/Qiniu.php

@@ -18,7 +18,7 @@ class Qiniu extends Core implements Config
 
         $options = array();
 
-        $ret = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config)->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
+        $ret = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file)->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
 
         if (isset($ret['hash']) && isset($ret['key'])) {
 
@@ -162,7 +162,12 @@ class Qiniu extends Core implements Config
             if (!strstr($source, 'http')) {
                 $source = $this->data['host'] . $source;
             }
-            $dest = $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
+            if (strstr($source, '?')) {
+                $prefix = '&';
+            } else {
+                $prefix = '?';
+            }
+            $dest = $source . $prefix . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
 
             if (isset($config['compress']) && $config['compress'] > 0) {
                 $dest .= '/q/' . $config['compress'];
@@ -200,7 +205,12 @@ class Qiniu extends Core implements Config
                 list($x, $y) = $this->img()->get_position($file['width'], $file['height'], $config['width'], $config['height'], 'crop', $config['type']);
             }
             
-            $dest = $source . '?imageMogr2/crop/!'.$config['width'].'x'.$config['height'].'a'.$x.'a' . $y;
+            if (strstr($source, '?')) {
+                $prefix = '&';
+            } else {
+                $prefix = '?';
+            }
+            $dest = $source . $prefix . 'imageMogr2/crop/!'.$config['width'].'x'.$config['height'].'a'.$x.'a' . $y;
 
             $dest .= '|imageslim';
         }

+ 11 - 4
lib/View/Oss.php

@@ -13,7 +13,7 @@ class Oss
 {
     private $client;
 
-    public function token($config, $upload)
+    public function token($config, $upload, $file = '')
     {
         $region = 'cn-' . $config['region_id'];
         if (!$config['role_arn']) {
@@ -69,13 +69,13 @@ class Oss
     }
 
     # 连接
-    public function connect($config, $upload)
+    public function connect($config, $upload, $file = '')
     {
         $accessKey = $config['appkey'];
         $secretKey = $config['appsecret'];
         $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
 
-        list($type, $token, $domain, $bucket) = $this->token($config, $upload);
+        list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
         $data = explode('||', $token);
         if ($data[0]) {
             $token = $data[0];
@@ -144,7 +144,14 @@ class Oss
     public function cover($key, $video, $num = 1)
     {
         $num = $num * 1000;
-        return $video . 'x-oss-process=video/snapshot,t_'.$num.',f_jpg,m_fast';
+        $file = $video . '?x-oss-process=video/snapshot,t_'.$num.',f_jpg,m_fast';
+        if ($local == 1) {
+            $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
+            return $data['url'] . '?time=' . time();
+        } else {
+            return $file;
+        }
+
         //x-oss-process=video/snapshot,t_1000,f_jpg,m_fast 
     }
 }

+ 15 - 7
lib/View/Qiniu.php

@@ -9,10 +9,11 @@ class Qiniu
     private $client;
     private $token;
 
-	public function token($config, $upload)
+	public function token($config, $upload, $file = '')
     {
         $cur = time();
-        if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
+        //if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
+        if (true) {
             $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
 
             # 暂时没用到callback
@@ -21,7 +22,7 @@ class Qiniu
                 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
             );
 
-            $token = $auth->uploadToken($upload['bucket'], null, 3600);
+            $token = $auth->uploadToken($upload['bucket'], $file, 3600);
             $up['token'] = $token;
             $up['token_endtime'] = $cur + 3600 - 60;
             $up['where_id'] = $config['id'];
@@ -45,9 +46,9 @@ class Qiniu
 	}
 
     # 连接
-    public function connect($config, $upload)
+    public function connect($config, $upload, $file = '')
     {
-        list($type, $token, $domain, $bucket) = $this->token($config, $upload);
+        list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
         $this->token = $token;
         // 构建 UploadManager 对象
         $this->client = new \Qiniu\Storage\UploadManager();
@@ -129,9 +130,16 @@ class Qiniu
     }
 
     # 视频截图
-    public function cover($key, $video, $num = 1)
+    public function cover($key, $video, $num = 1, $local = 2)
     {
-        return $video . '?vframe/jpg/offset/' . $num;
+        $file = $video . '?vframe/jpg/offset/' . $num;
+        if ($local == 1) {
+            $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
+            return $data['url'] . '?time=' . time();
+        } else {
+            return $file;
+        }
+        
         //vframe/jpg/offset/7/w/480/h/360
     }
 }

+ 5 - 1
src/Save.php

@@ -71,7 +71,7 @@ class Save
 		}
 	}
 
-	public function copy($file = false, $key = 1, $state = false, $search = '')
+	public function copy($file = false, $key = 1, $state = false, $search = '', $ext = false)
 	{
 		if ($search == 'undefined') {
 			$search = '';
@@ -83,6 +83,10 @@ class Save
 			$image['file'] 	= $file;
 			$image['key'] 	= $key;
 			$image['search'] = $search;
+			if ($ext) {
+				$image['ext'] = $ext;
+			}
+			
 			$handle = new Handle($image);
 
 			$this->output = $handle->copy();

+ 39 - 5
src/View.php

@@ -174,7 +174,7 @@ class View
                 } elseif (!isset($v['ext'])) {
                     $data['file'][$k]['pic'] = $pic;
                 } else {
-                    if ($v['ext'] != '.jpg' && $v['ext'] != '.png' && $v['ext'] != '.bmp' && $v['ext'] != '.gif') {
+                    if ($v['ext'] != '.jpg' && $v['ext'] != '.png' && $v['ext'] != '.bmp' && $v['ext'] != '.gif' && $v['ext'] != '.webp') {
                         $data['file'][$k]['pic'] = $pic;
                     } else {
                         $data['file'][$k]['pic'] = $data['file'][$k]['url'];
@@ -204,10 +204,21 @@ class View
         $data['cur'] = Dever::input('cur');
         $data['cover'] = Dever::input('cover');
 
-        $data['cover_pic'] = Dever::load('upload/yun')->cover($data['key'], $data['cur']);
+        $video = parse_url($data['cur']);
+        $file = ltrim($video['path'], '/');
+        $file = Dever::db('upload/file')->one(array('file' => $file));
+        if ($file && $file['pic']) {
+            $data['num'] = $file['pic_num'];
+            $data['cover_pic'] = $file['pic'];
+        } else {
+            $data['num'] = 1;
+            $data['cover_pic'] = Dever::load('upload/yun')->cover($data['key'], $data['cur'], $data['num']);
+        }
 
         $data['video'] = Dever::url('view.video?video=' . Dever::encode($data['cur']), 'upload');
-        $data['cover_pic_url'] = Dever::url('view.coverPic?key='.$data['key'].'&video=' . Dever::encode($data['cur']), 'upload');
+
+        $data['cover_pic_url'] = Dever::url('view.coverPic?&key='.$data['key'].'&video=' . Dever::encode($data['cur']), 'upload');
+
         $data = Dever::render('cover', $data);
         Dever::out($data);
     }
@@ -217,7 +228,7 @@ class View
     {
         $video = Dever::decode(Dever::input('video'));
 
-        return '<video src="'.$video.'" height="200" controls="true"/>';
+        return '<video src="'.$video.'" width="300" controls="true"/>';
     }
 
     # 展示视频截图
@@ -226,11 +237,34 @@ class View
         $num = Dever::input('num');
         $key = Dever::input('key');
         $video = Dever::decode(Dever::input('video'));
-        $pic = Dever::load('upload/yun')->cover($key, $video, $num);
+        $local = Dever::input('local', 2);
+        $pic = Dever::load('upload/yun')->cover($key, $video, $num, $local);
 
         return $pic;
     }
 
+    public function copy()
+    {
+        $file = Dever::input('file');
+        $video = Dever::input('video');
+        $num = Dever::input('num');
+        $data = Dever::load('upload/save')->copy($file, 7, true, false, 'jpg');
+
+        $result = $data['url'] . '?t=' . Dever::rand(10);
+        if ($video) {
+            $video = parse_url($video);
+            $file = ltrim($video['path'], '/');
+            $file = Dever::db('upload/file')->one(array('file' => $file));
+            if ($file) {
+                $update['where_id'] = $file['id'];
+                $update['pic'] = $result;
+                $update['pic_num'] = $num;
+                Dever::db('upload/file')->update($update);
+            }
+        }
+        return $result;
+    }
+
     public function setState()
     {
         $id = Dever::input('id');

+ 2 - 2
src/Yun.php

@@ -21,7 +21,7 @@ class Yun
 	}
 
     # 查看视频截图
-    public function cover($key, $video, $num = 1)
+    public function cover($key, $video, $num = 1, $local = 2)
     {
         $domain = parse_url($video);
         $host = $domain['scheme'] . '://' . $domain['host'] . '/';
@@ -29,7 +29,7 @@ class Yun
         $config = Dever::db('upload/yun')->one(array('host' => $host));
 
         if ($config) {
-            return $this->getMethod($config['type'])->cover($key, $video, $num);
+            return $this->getMethod($config['type'])->cover($key, $video, $num, $local);
         }
 
         return false;