|
@@ -1,27 +1,96 @@
|
|
|
<?php
|
|
|
+
|
|
|
/**
|
|
|
- * upload文件浏览器
|
|
|
+ * upload文件管理
|
|
|
*
|
|
|
* @version 0.0.0
|
|
|
* @author by huwhois
|
|
|
* @time 2017/11/27
|
|
|
*/
|
|
|
-namespace app\admin\controller;
|
|
|
|
|
|
-use think\App;
|
|
|
-use think\Request;
|
|
|
+namespace app\sys\controller;
|
|
|
+
|
|
|
use think\facade\View;
|
|
|
+use think\File;
|
|
|
use think\Image;
|
|
|
+use think\facade\Config;
|
|
|
+
|
|
|
+use app\common\service\FileService;
|
|
|
+use Exception;
|
|
|
|
|
|
class FileManager extends Base
|
|
|
{
|
|
|
- protected $img_path = '';
|
|
|
protected $t_suffix = '_thumb';
|
|
|
- protected $t_width = 400;
|
|
|
- protected $t_height = 300;
|
|
|
- protected $request;
|
|
|
+ protected $width = 400; // 缩略图高度
|
|
|
+ protected $height = 300;
|
|
|
|
|
|
- public function uploadimg($thumb=false, $width=400, $height=300)
|
|
|
+ /**
|
|
|
+ * 处理上传的图片
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ // halt(Config::get('filesystem.disks.public.root') .'/' . $thumbname);
|
|
|
+ $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
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图片上传
|
|
|
+ * @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 生成缩略图后是否保存原图
|
|
|
+ */
|
|
|
+ public function uploadimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
|
$file = $this->request->file('upload_file');
|
|
@@ -30,10 +99,10 @@ class FileManager extends Base
|
|
|
validate(
|
|
|
[
|
|
|
'file' => [
|
|
|
- // 限制文件大小(单位b),这里限制为20M
|
|
|
- 'fileSize' => 20 * 1024 * 1024,
|
|
|
+ // 限制文件大小(单位b),这里限制为4M
|
|
|
+ 'fileSize' => 4 * 1024 * 1024,
|
|
|
// 限制文件后缀,多个后缀以英文逗号分割
|
|
|
- 'fileExt' => 'jpg,png,gif,jpeg,webp'
|
|
|
+ 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
|
|
|
]
|
|
|
],
|
|
|
[
|
|
@@ -42,54 +111,150 @@ class FileManager extends Base
|
|
|
]
|
|
|
)->check(['file' => $file]);
|
|
|
|
|
|
- $savename = \think\facade\Filesystem::disk('public')->putFile( '/', $file);
|
|
|
- $thumbname = "";
|
|
|
- if ($thumb) {
|
|
|
- $thumbname = $this->makeThumb($savename, $width, $height);
|
|
|
- }
|
|
|
+ $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);
|
|
|
} catch (\think\exception\ValidateException $e) {
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
- unset($file);
|
|
|
- return [
|
|
|
- 'code'=>2,
|
|
|
- 'picname' => '/storage/' . str_replace('\\', '/', $savename),
|
|
|
- 'thumbname'=>'/storage/' . $thumbname
|
|
|
- ];
|
|
|
} else {
|
|
|
- $this->error('图片不能为空');
|
|
|
+ $this->error('图片不能为空');
|
|
|
}
|
|
|
} else {
|
|
|
+ View::assign('img_id', $img_id);
|
|
|
+ View::assign('water', $water);
|
|
|
View::assign('thumb', $thumb);
|
|
|
View::assign('width', $width);
|
|
|
View::assign('height', $height);
|
|
|
+ View::assign('overwrite', $overwrite);
|
|
|
|
|
|
return View::fetch();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 生成缩略图
|
|
|
- * @param string $filename 必须参数, 图片路径名(相对or绝对)(/public/uploads/...)
|
|
|
- * @param int $width 缩略图宽值, 默认 384px;
|
|
|
- * @param int $height 缩略图高值, 默认 224px;
|
|
|
- * @param int $type 缩略图裁剪方式, 默认值 1, 固定尺寸缩放; 其他: 1, 等比例缩放;
|
|
|
- * 2, 缩放后填充; 3, 居中裁剪; 4, 左上角裁剪; 5, 右下角裁剪
|
|
|
- * @return string $thumbname 缩略图文件名
|
|
|
+ * 图片上传
|
|
|
+ * @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 生成缩略图后是否保存原图
|
|
|
*/
|
|
|
- public function makeThumb($filename, $width =384, $height = 224, $type = 1)
|
|
|
+ public function uploadurlimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
{
|
|
|
- $file ='./storage/' . str_replace('\\', '/', $filename);
|
|
|
+ if ($this->request->isPost()) {
|
|
|
+ $urlImg = $this->request->param('url_file');
|
|
|
+
|
|
|
+ if ($urlImg) {
|
|
|
+ try {
|
|
|
+ $fileService = new FileService();
|
|
|
+
|
|
|
+ $file = $fileService->urlImg($urlImg);
|
|
|
+
|
|
|
+ $arr = $this->dealUploadImg($file, $water, $thumb, $width, $height, $overwrite);
|
|
|
+
|
|
|
+ @unlink($file->realPath);
|
|
|
+
|
|
|
+ return array_merge([
|
|
|
+ 'code' => 0,
|
|
|
+ 'img_id' => $img_id,
|
|
|
+ 'picture_url' => Config::get('filesystem.disks.public.url') . '/']
|
|
|
+ , $arr);
|
|
|
+ } catch (\think\exception\ValidateException $e) {
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $this->error('图片地址不能为空');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function uploadonlineimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false)
|
|
|
+ {
|
|
|
+ if ($this->request->isPost()) {
|
|
|
+ $pathImg = $this->request->param('online_file');
|
|
|
+
|
|
|
+ if ($pathImg && file_exists($this->app->getRootPath() . "public" . $pathImg)) {
|
|
|
+
|
|
|
+ $picname = $pathImg;
|
|
|
+ $thumbname = "";
|
|
|
+
|
|
|
+ if ($thumb) {
|
|
|
+ if (stripos($picname, $this->t_suffix)) {
|
|
|
+ $thumbname = $pathImg;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ $file = new File($this->app->getRootPath() . "public" . $pathImg);
|
|
|
+
|
|
|
+ $ext = $file->getExtension();
|
|
|
+
|
|
|
+ $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $picname)) . $this->t_suffix . '.' . $ext;
|
|
|
+
|
|
|
+ if (!file_exists($thumbname)) {
|
|
|
+ $image = Image::open($file);
|
|
|
|
|
|
- $ext = pathinfo($file, PATHINFO_EXTENSION);
|
|
|
+ $image->thumb($width, $height, 1);
|
|
|
+
|
|
|
+ $image->save($this->app->getRootPath() . "public" . $thumbname);
|
|
|
+ unset($image);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($overwrite) {
|
|
|
+ $picname = $thumbname;
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'code' => 0,
|
|
|
+ 'img_id' => $img_id,
|
|
|
+ 'picname' => $picname,
|
|
|
+ 'thumbname' => $thumbname,
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $this->error('图片地址不存在');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $this->t_suffix . '.' . $ext;
|
|
|
+ public function onlineimg($page)
|
|
|
+ {
|
|
|
+ $files = FileService::getFiles(Config::get('filesystem.disks.public.root'));
|
|
|
+
|
|
|
+ if (!count($files)) {
|
|
|
+ return json_encode(array(
|
|
|
+ "state" => "no match file",
|
|
|
+ "list" => array(),
|
|
|
+ "start" => $page,
|
|
|
+ "total" => count($files)
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ /* 获取指定范围的列表 */
|
|
|
+ $page = $page-1 < 0 ? 0 : (int)$page - 1;
|
|
|
+ $size = 20;
|
|
|
+ $start = $page * $size;
|
|
|
+ $end = $start + $size;
|
|
|
|
|
|
- $image = Image::open($file);
|
|
|
+ $len = count($files);
|
|
|
|
|
|
- $result = $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);
|
|
|
+ for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
|
|
|
+ $list[] = $files[$i];
|
|
|
+ }
|
|
|
|
|
|
- return $result ? $thumbname : '';
|
|
|
+ return json([ "code" => 0,
|
|
|
+ "list" => $list,
|
|
|
+ "page" => $page+1,
|
|
|
+ "total" => count($files)
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
public function index()
|
|
@@ -112,19 +277,19 @@ class FileManager extends Base
|
|
|
protected function explorer()
|
|
|
{
|
|
|
$param = $this->request->param();
|
|
|
-
|
|
|
- $activepath = isset($param['activepath'])? $param['activepath'] : '';
|
|
|
-
|
|
|
+
|
|
|
+ $activepath = isset($param['activepath']) ? $param['activepath'] : '';
|
|
|
+
|
|
|
$inpath = "";
|
|
|
|
|
|
$inpath = $this->img_path . $activepath;
|
|
|
|
|
|
$dirhandle = scandir($inpath);
|
|
|
-
|
|
|
+
|
|
|
$dirs = $files = [];
|
|
|
|
|
|
define('BKM', 1024);
|
|
|
-
|
|
|
+
|
|
|
foreach ($dirhandle as $val) {
|
|
|
if ($val == "." || $val == "..") {
|
|
|
continue;
|
|
@@ -134,24 +299,24 @@ class FileManager extends Base
|
|
|
$arr = [];
|
|
|
$file = '';
|
|
|
$file = $inpath . DIRECTORY_SEPARATOR . $val;
|
|
|
-
|
|
|
+
|
|
|
$arr['name'] = $val;
|
|
|
-
|
|
|
+
|
|
|
$arr['extension'] = pathinfo($file, PATHINFO_EXTENSION);
|
|
|
|
|
|
$filesize = floatval(filesize($file));
|
|
|
- if ($filesize>BKM) {
|
|
|
- $filesize = round($filesize/BKM, 2);
|
|
|
+ if ($filesize > BKM) {
|
|
|
+ $filesize = round($filesize / BKM, 2);
|
|
|
+
|
|
|
+ if ($filesize > BKM) {
|
|
|
+ $filesize = round($filesize / BKM, 2);
|
|
|
|
|
|
- if ($filesize>BKM) {
|
|
|
- $filesize = round($filesize/BKM, 2);
|
|
|
-
|
|
|
- $filesize .="M";
|
|
|
+ $filesize .= "M";
|
|
|
} else {
|
|
|
- $filesize .="K";
|
|
|
+ $filesize .= "K";
|
|
|
}
|
|
|
} else {
|
|
|
- $filesize .="B";
|
|
|
+ $filesize .= "B";
|
|
|
}
|
|
|
$arr['size'] = $filesize;
|
|
|
|
|
@@ -161,16 +326,16 @@ class FileManager extends Base
|
|
|
$files[] = $arr;
|
|
|
}
|
|
|
}
|
|
|
- $counts = count($dirs)+count($files);
|
|
|
+ $counts = count($dirs) + count($files);
|
|
|
|
|
|
$activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);
|
|
|
-
|
|
|
+
|
|
|
$data = [
|
|
|
'dirs' => $dirs,
|
|
|
'files' => $files,
|
|
|
'counts' => $counts,
|
|
|
'activeurl' => $activeurl,
|
|
|
- 'activepath'=> $activepath,
|
|
|
+ 'activepath' => $activepath,
|
|
|
];
|
|
|
|
|
|
return $data;
|
|
@@ -179,24 +344,24 @@ class FileManager extends Base
|
|
|
public function delDir()
|
|
|
{
|
|
|
if ($this->request->isAjax()) {
|
|
|
- $activepath = $this->request->param('activepath');
|
|
|
+ $activepath = $this->request->param('activepath');
|
|
|
$activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);
|
|
|
$dir = $this->request->param('dir');
|
|
|
|
|
|
- $dir_name = app()->getRootPath.'public'.DIRECTORY_SEPARATOR.$activepath.DIRECTORY_SEPARATOR.$dir;
|
|
|
-
|
|
|
+ $dir_name = app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . $activepath . DIRECTORY_SEPARATOR . $dir;
|
|
|
+
|
|
|
if (count(scandir($dir_name)) > 2) {
|
|
|
- return ['status'=>1,'msg'=>'不可删除非空目录'];
|
|
|
+ return ['status' => 1, 'msg' => '不可删除非空目录'];
|
|
|
}
|
|
|
|
|
|
if (rmdir($dir_name)) {
|
|
|
- return ['status'=>2,'msg'=>'success'];
|
|
|
+ return ['status' => 2, 'msg' => 'success'];
|
|
|
} else {
|
|
|
- return ['status'=>0,'msg'=>'failed'];
|
|
|
+ return ['status' => 0, 'msg' => 'failed'];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function del()
|
|
|
{
|
|
|
if ($this->request->isAjax()) {
|
|
@@ -204,10 +369,10 @@ class FileManager extends Base
|
|
|
$activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);
|
|
|
$filename = $this->request->param('filename');
|
|
|
|
|
|
- if (unlink(app()->getRootPath.'public'.DIRECTORY_SEPARATOR.$activepath.DIRECTORY_SEPARATOR.$filename)) {
|
|
|
- return ['status'=>1,'msg'=>'success'];
|
|
|
+ if (unlink(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . $activepath . DIRECTORY_SEPARATOR . $filename)) {
|
|
|
+ return ['status' => 1, 'msg' => 'success'];
|
|
|
} else {
|
|
|
- return ['status'=>0,'msg'=>'failed'];
|
|
|
+ return ['status' => 0, 'msg' => 'failed'];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -226,13 +391,13 @@ class FileManager extends Base
|
|
|
// if (!$info) {
|
|
|
// $this->error($info->getError());
|
|
|
// }
|
|
|
-
|
|
|
+
|
|
|
// $filename = $info->getSaveName();
|
|
|
// unset($info);
|
|
|
// if ($isthumb == 1) {
|
|
|
// $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;
|
|
|
// $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;
|
|
|
-
|
|
|
+
|
|
|
// $thumbname = $this->makeThumb($filename, $width, $height);
|
|
|
|
|
|
// if (!$thumbname) {
|
|
@@ -258,7 +423,7 @@ class FileManager extends Base
|
|
|
*/
|
|
|
protected function saveUpload($file, $mode)
|
|
|
{
|
|
|
- $validate = ['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'];
|
|
|
+ $validate = ['size' => 2097152, 'ext' => 'jpg,png,gif,jpeg'];
|
|
|
|
|
|
if ($mode) {
|
|
|
$upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads', '');
|
|
@@ -304,7 +469,7 @@ class FileManager extends Base
|
|
|
// } else {
|
|
|
// $basepath = \config('url_domain_root') . '/uploads';
|
|
|
// // var_dump($param);
|
|
|
-
|
|
|
+
|
|
|
// if ($isthumb!=1 && preg_match("#" . $basepath . "#i", $urlimg)) {
|
|
|
// $this->error('图片已在服务其中, 可直接选用');
|
|
|
// }
|
|
@@ -317,7 +482,7 @@ class FileManager extends Base
|
|
|
// $this->error('下载失败, 请稍后重试');
|
|
|
// }
|
|
|
// }
|
|
|
-
|
|
|
+
|
|
|
// if ($formername==1) {
|
|
|
// // 获取原文件名
|
|
|
// $fileinfo = pathinfo($urlimg);
|
|
@@ -328,15 +493,15 @@ class FileManager extends Base
|
|
|
// } else {
|
|
|
// $filename = '';
|
|
|
// }
|
|
|
-
|
|
|
+
|
|
|
// $filename = \mylib\GetImageByurl::getImageByurl($urlimg, $filename, $savePath);
|
|
|
-
|
|
|
+
|
|
|
// if ($filename) {
|
|
|
// // 生成缩略图
|
|
|
// if ($isthumb==1) {
|
|
|
// $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;
|
|
|
// $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;
|
|
|
-
|
|
|
+
|
|
|
// $thumbname = $this->makeThumb($today . DIRECTORY_SEPARATOR . $filename, $width, $height);
|
|
|
|
|
|
// if (!$thumbname) {
|