huwhois vor 2 Jahren
Ursprung
Commit
81bcdf0355

+ 2 - 1
.gitignore

@@ -10,4 +10,5 @@ composer.lock
 /public/index
 /public/static/plugins/ueditor
 /public/static/plugins/Swiper
-/public/static/plugins/jquery.SuperSlide
+/public/static/plugins/jquery.SuperSlide
+/public/storage

+ 1 - 1
app/common/command/FileConsole.php

@@ -55,7 +55,7 @@ class FileConsole extends Command
         if (is_file($dir)) {
             $file = new File($dir);
 
-            FileManager::saveFileInfo($file);
+            FileManager::saveFile($file);
         } else {
             $dirs = new DirectoryIterator($dir);
             

+ 41 - 10
app/common/model/FileManager.php

@@ -4,6 +4,9 @@ declare(strict_types=1);
 namespace app\common\model;
 
 use think\facade\Config;
+use think\File;
+use think\Image;
+use think\exception\FileException;
 
 class FileManager extends \think\Model
 {
@@ -20,7 +23,6 @@ class FileManager extends \think\Model
         'type'          => "int",      // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
         'onclick'       => "int",      // '下载量'
         'username'      => "varchar",  // '上传者'
-        'cid'           => "int",      // '栏目ID'
         'infoid'        => "int",      // '信息ID', 
         'cjid'          => "int",       // '信息临时ID'
         'hash_md5'       => "varcahr"       // hash值(MD5)
@@ -34,10 +36,10 @@ class FileManager extends \think\Model
 
         $where = [];
 
-        return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->paginate(['list_rows'=>$limit, 'query' => $param]);
+        return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->paginate(['list_rows'=>$limit, 'query' => $param]);
     }
 
-    public static function saveFileInfo(\think\File $file)
+    public static function saveFile(\think\File $file)
     {
         $fileinfo = self::where('hash_md5', $file->md5())->find();
 
@@ -51,13 +53,42 @@ class FileManager extends \think\Model
             $fileinfo = new static();
         }
 
-        $fileinfo->filename       = $file->getFilename();
-        $fileinfo->filesize       = $file->getSize();
-        $fileinfo->filetime       = $file->getCTime();
-        $fileinfo->filepath       = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
-        $fileinfo->fileextension  = $file->getExtension();
-        $fileinfo->username       = 'system';
-        $fileinfo->hash_md5       = $file->md5();
+        $fileinfo->filename      = $file->getFilename();
+        $fileinfo->filesize      = $file->getSize();
+        $fileinfo->filetime      = $file->getCTime();
+        $fileinfo->filepath      = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
+        $fileinfo->fileextension = $file->getExtension();
+        $fileinfo->hash_md5      = $file->md5();
+        $fileinfo->username      = 'system';
+
+        $fileinfo->save();
+    }
+
+    public static function saveFileInfo($file, $id = 0, $cjid = 0, $username = 'system')
+    {
+        if (is_string($file)) {
+            $file = new \SplFileInfo($file);
+        }
+        if (!$file->isFile()) {
+            throw new FileException('file not exist');
+        }
+
+        $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));
+
+        $publicUrlPath =  Config::get('filesystem.disks.public.url');
+
+        
+        $fileinfo = new static();
+        
+        $fileinfo->filename      = $file->getFilename();
+        $fileinfo->filesize      = $file->getSize();
+        $fileinfo->filetime      = $file->getCTime();
+        $fileinfo->filepath      = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
+        $fileinfo->fileextension = $file->getExtension();
+        $fileinfo->hash_md5      = $file->md5();
+        $fileinfo->username      = $id;
+        $fileinfo->username      = $cjid;
+        $fileinfo->username      = $username;
 
         $fileinfo->save();
     }

+ 3 - 13
app/common/service/FileService.php

@@ -30,11 +30,9 @@ class FileService
      * @param string $fontcolor  水印文字颜色
      * @return Image  返回图片对象
      */
-    public static function waterMark(File $file, int $type = 0, string $watermark = '', string $watertext = '', 
+    public static function waterMark(Image $image, int $type = 0, string $watermark = '', string $watertext = '', 
         string $fonttype = '', int $fontsize = 0, string $fontcolor = '#ffffff30'): Image
     {
-        $image = Image::open($file);
-
         if ($type == 0) {
             $watermark = $watermark ?: Config::get('filesystem.water.watermark');
             $image->water($watermark);
@@ -52,23 +50,15 @@ class FileService
 
     /**
      * 生成缩略图
-     * @param File $file  要处理的文件
+     * @param Image $image  要处理的文件
      * @param int $width 缩略图宽值, 默认 384px;
      * @param int $height 缩略图高值, 默认 224px;
      * @param int $type 缩略图裁剪方式, 默认值 1,固定尺寸缩放; 其他: 1,等比例缩放;2,缩放后填充;3,居中裁剪;4,左上角裁剪;5,右下角裁剪
      * @param string $t_suffix  缩略图后缀
      * @return Image  返回图片对象
      */
-    public static function thumbnail(File $file, int $width = 384, int $height = 224, int $type = 1, string $t_suffix = "thumb")
+    public static function thumbnail(Image $image, string $thumbname, int $width = 384, int $height = 224, int $type = 1)
     {
-        $image = Image::open($file);
-
-        $ext = $file->getExtension();
-
-        $filename = $file->getFilename();
-
-        $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $t_suffix . '.' . $ext;
-
         $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
 
         return $image;

+ 104 - 83
app/sys/controller/FileManager.php

@@ -32,7 +32,7 @@ class FileManager extends Base
     protected $width = 400;  // 缩略图高度
     protected $height = 300;
     protected $storage_path = 'public/storage';
-
+    
     public function index()
     {
         $param = $this->request->param();
@@ -144,7 +144,7 @@ class FileManager extends Base
                     $fileinfo->hash_md5      = $file->md5();
 
                     if (empty($activepath)) {
-                        $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);                
+                        $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
                         
                         $fileinfo->filepath = $url . '/' . $savename;
                         $fileinfo->filename = basename($savename);  
@@ -259,74 +259,20 @@ class FileManager extends Base
         }
     }
 
-    /**
-     * 处理上传的图片
-     */
-    protected function dealUploadImg(File $file, $water, $thumb, $width, $height, $overwrite)
-    {
-        $savename = "";
-        $thumbname = "";
-
-        if ($water == true || $thumb == true) {
-            $image = Image::open($file);
-
-            if ($water) {
-                $type = $this->system->water_type ?: Config::get('filesystem.water.type');
-
-                if ($type == 'water') {
-                    $watemark = $this->system->watermark ?: Config::get('filesystem.water.watermark');
-                    $image->water($watemark);
-                } else {
-                    $watetext = $this->system->watertext ?: Config::get('filesystem.water.watertext');
-                    $image->text($watetext, Config::get('filesystem.water.waterfont'), 30, '#ffffff30');
-                }
-            }
-
-            $savename = $file->hashName();
-            $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;
-
-            if ($thumb == true) {
-                if ($overwrite == true) {
-                    $image->thumb($width, $height, 1);
-
-                    $image->save($realPath);
-                } else {
-                    $image->save($realPath);
-
-                    $image->thumb($width, $height, 1);
-
-                    $ext = $file->extension();
-
-                    $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;
-
-                    $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);
-                }
-            } else {
-                $image->save($realPath);
-            }
-            unset($image);
-        } else {
-            $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
-        }
-
-        return [
-            'picname'   => str_replace('\\', '/', $savename),
-            'thumbname' => $thumbname
-        ];
-    }
-
     /**
      * 图片上传(iframe 页面)
      */
-    public function uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
+    public function uploadimg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
+        $original = false, $id = 0, $cjid = 0)
     {
         View::assign([
-            'img_id' => $img_id,
-            'water'  => $water,
-            'thumb'  => $thumb,
-            'width'  => $width,
-            'height' => $height,
-            'overwrite' => $overwrite
+            'img_id'    => $img_id,
+            'thumb'     => $thumb,
+            'width'     => $width,
+            'height'    => $height,
+            'original'  => $original,
+            'id'        => $id,
+            'cjid'      => $cjid
         ]);
 
         return View::fetch();
@@ -336,18 +282,21 @@ class FileManager extends Base
      * 本地图片上传
      * @file upload_file   上传的文件
      * @param string  $img_id   图片ipnut text id 默认值 picture
-     * @param boolean $water    是否添加水印
      * @param boolean $thumb    是否制作缩略图
      * @param int $width        缩略图最大宽
      * @param int $height       缩略图最大高
-     * @param bool $overwrite   生成缩略图后是否保存原图
+     * @param int $id           信息ID
+     * @param int $cjid         信息临时ID
+     * @param bool $saveoriginal   生成缩略图后是否保存原图 默认保存 saveoriginal
      */
-    public function uploadLocalImg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
+    public function uploadLocalImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
+        $original = false, $id = 0, $cjid = 0)
     {
         if ($this->request->isPost()) {
             $file = $this->request->file('upload_file');
+
             if ($file) {
-                try {
+                // try {
                     validate(
                         [
                             'file' => [
@@ -362,21 +311,55 @@ class FileManager extends Base
                             'file.fileExt' => '不支持的文件后缀',
                         ]
                     )->check(['file' => $file]);
-                
-                    $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
 
-                    return array_merge(
-                        [
-                            'code' => 0,
-                            'img_id' => $img_id,
-                            'picture_url' => Config::get('filesystem.disks.public.url') . '/'
-                        ],
-                        $arr
-                    );
+                    $savename =  str_replace('\\', '/', $file->hashName()) ;
 
-                } catch (ValidateException $e) {
-                    $this->error($e->getMessage());
-                }
+
+
+                    if ($thumb == true) {
+                        $image = Image::open($file);
+                        
+                        $image->thumb($width, $height, 1);
+
+                        $ext = $file->extension();
+
+                        $thumbname = str_replace('.' . $ext, '',  $savename) . $this->t_suffix . '.' . $ext;    
+
+                        if ($original == true) {
+                            \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
+                        } else {
+                            $thumbname = str_replace('.' . $ext, '',  $thumbname) . $this->t_suffix . '.' . $ext;
+                            $savename = $thumbname;
+                        }
+                        $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);                      
+                    } else {
+                        \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
+                    }
+
+                    halt($savename);
+
+                    $fileinfo = new FileManagerModel();
+                    
+                    $fileinfo->title         = $file->getOriginalName();
+                    $fileinfo->filesize      = $file->getSize();
+                    $fileinfo->filetime      = $file->getCTime();
+                    $fileinfo->fileextension = $file->extension();
+                    $fileinfo->hash_md5      = $file->md5();
+                    $fileinfo->filepath      = Config::get('filesystem.disks.public.url') . '/' . $savename;
+                    $fileinfo->filename      = basename($savename); 
+                    $fileinfo->username      = $this->getSysUser()->username;
+                    $fileinfo->infoid        =  $id;
+                    $fileinfo->cjid          =  $cjid;
+                    
+                    $fileinfo->save();
+                    // $res = FileManagerModel::dealUploadImg($file, $savename, $water, $thumb, $width, $height, $overwrite, $id, $cjid);
+
+                    // return json(array_merge(['code' => 0, 'img_id' => $img_id,], $res));
+                // } catch (ValidateException $e) {
+                //     $this->error($e->getMessage());
+                // } catch (Exception $e) {
+                //     $this->error($e->getMessage());
+                // }
             } else {
                 $this->error('图片不能为空');
             }
@@ -404,7 +387,7 @@ class FileManager extends Base
 
                     $file = $fileService->downloadUrlImg($urlImg);
 
-                    $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
+                    $arr = $this->dealUploadImg($file, $thumb, $width, $height, $overwrite);
 
                     @unlink($file->realPath);
 
@@ -581,4 +564,42 @@ class FileManager extends Base
             }
         }
     }
+
+    /**
+     * 处理上传的图片
+     */
+    protected function dealUploadImg(File $file, $thumb, $width, $height, $overwrite, $id = 0, $cjid = 0)
+    {
+        $savename = $file->hashName();
+
+        if ($thumb == true) {
+            $image = Image::open($file);
+            
+            if ($overwrite == true) {
+                $image->thumb($width, $height, 1);
+            
+                $realPath = Config::get('filesystem.disks.public.root') . '/' . $savename;
+
+                $image->save($realPath);
+            } else {
+                
+                $ext = $file->extension();
+
+                $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $savename)) . $this->t_suffix . '.' . $ext;
+                
+                $image->save(Config::get('filesystem.disks.public.root') . '/' . $thumbname);
+            }
+
+            unset($image);
+        } else {
+            $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
+        }
+
+        $picture_url = Config::get('filesystem.disks.public.url') . '/';
+
+        return [
+            'picname'   => $picture_url . str_replace('\\', '/', $savename),
+            'thumbname' => $thumbname ? $picture_url . $thumbname : ""
+        ];
+    }
 }

+ 0 - 2
public/storage/.gitignore

@@ -1,2 +0,0 @@
-*
-!.gitignore

+ 3 - 0
view/sys/file_manager/index.html

@@ -75,6 +75,9 @@
             </tbody>
         </table>
     </div>
+    <div class="cl pd-5 bg-1 bk-gray mt-20 ">
+        <span class="r">{$list->render()|raw}</span>
+    </div>
 </article>
 
 <script>

+ 37 - 37
view/sys/file_manager/uploadimg.html

@@ -109,25 +109,23 @@
                 </div>
                 <div class="row cl" style="margin-top:20px;">
                     <input type="hidden" name="img_id" value={$img_id}>
-                    <div class="formControls col-xs-12 col-sm-8">
+                    <input type="hidden" name="id" value={$id}>
+                    <input type="hidden" name="cjid" value={$cjid}>
+                    <div class="formControls col-sm-8">
                         <div class="skin-minimal">
                             <div class="check-box">
-                                <input type="checkbox" name="overwrite" value="{$overwrite}" {$overwrite ? 'checked'
-                                    : "" }>
-                                <label for="overwrite">覆盖原图</label>
-                            </div>
-                            <div class="check-box">
-                                <input type="checkbox" name="water" value="{$water}" {$water ? 'checked' : "" }>
-                                <label for="water">水印</label>
-                            </div>
-                            <div class="check-box">
-                                <input type="checkbox" name="thumb" value="{$thumb}" {$thumb ? 'checked' : "" }>
+                                <input type="checkbox" name="thumb" value="true" {$thumb ? 'checked' : "" }>
                                 <label for="thumb-1">缩略图</label>
                                 <input type="text" class="input-text" name="width" value={$width}
                                     style="width: 80px;">缩略图宽度
                                 <input type="text" class="input-text" name="height" value={$height}
                                     style="width: 80px;">缩略图高度
                             </div>
+                            <div class="check-box">
+                                <input type="checkbox" name="original" value="true" {$original ? 'checked'
+                                    : "" }>
+                                <label for="overwrite">保留原图</label>
+                            </div>
                         </div>
                     </div>
                 </div>
@@ -184,31 +182,6 @@
         })
     });
 
-    function onlinepicture(page) {
-        var data = { "page": page };
-        $.ajax({
-            url: '{:url("file_manager/onlineimg")}',
-            type: 'GET',
-            async: true,
-            cache: false,
-            data: 'page=' + page,
-            processData: false,
-            contentType: false,
-            dataType: "json",
-            success: function (res) {
-                if (res.code == 0) {
-                    html_str = "";
-                    res.list.forEach(element => {
-                        html_str += '<li><img width="170" height="113" src="' + element.url + '?noCache=' + element.mtime + '"';
-                        html_str += '_src="' + element.url + '">';
-                        html_str += '<span class="icon"></span></li>';
-                    });
-                    $('ul.list').prepend(html_str);
-                }
-            }
-        });
-    }
-
     //step1本地上传图片
     function uploadImg() {
         if ($("#upload_file").val() == '') {
@@ -220,7 +193,7 @@
         } else {
             var formData = new FormData($("#form-uploadimg")[0]);
             $.ajax({
-                url: '{:url("file_manager/uploadimg")}',
+                url: '{:url("file_manager/uploadLocalImg")}',
                 type: 'POST',
                 async: true,
                 cache: false,
@@ -258,6 +231,33 @@
         }
     }
 
+
+
+    function onlinepicture(page) {
+        var data = { "page": page };
+        $.ajax({
+            url: '{:url("file_manager/onlineimg")}',
+            type: 'GET',
+            async: true,
+            cache: false,
+            data: 'page=' + page,
+            processData: false,
+            contentType: false,
+            dataType: "json",
+            success: function (res) {
+                if (res.code == 0) {
+                    html_str = "";
+                    res.list.forEach(element => {
+                        html_str += '<li><img width="170" height="113" src="' + element.url + '?noCache=' + element.mtime + '"';
+                        html_str += '_src="' + element.url + '">';
+                        html_str += '<span class="icon"></span></li>';
+                    });
+                    $('ul.list').prepend(html_str);
+                }
+            }
+        });
+    }
+
     // 网络图片
     function uploadUrlImg() {
         if ($("#url_file").val() == '') {