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'); if ($file) { try { validate( [ 'file' => [ // 限制文件大小(单位b),这里限制为4M 'fileSize' => 4 * 1024 * 1024, // 限制文件后缀,多个后缀以英文逗号分割 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif' ] ], [ 'file.fileSize' => '文件太大', '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); } catch (\think\exception\ValidateException $e) { $this->error($e->getMessage()); } } else { $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(); } } /** * 图片上传 * @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 uploadurlimg(string $img_id = 'picture', $water = false, $thumb = false, $width = 400, $height = 300, $overwrite = false) { 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); $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('图片地址不存在'); } } } 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; $len = count($files); for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){ $list[] = $files[$i]; } return json([ "code" => 0, "list" => $list, "page" => $page+1, "total" => count($files) ]); } public function index() { $data = $this->explorer(); View::assign('dirs', $data['dirs']); View::assign('files', $data['files']); View::assign('counts', $data['counts']); View::assign('activeurl', $data['activeurl']); View::assign('activepath', $data['activepath']); return View::fetch(); } /** * 获取当前文件相关信息 * @return array $data 文件信息数据组 */ protected function explorer() { $param = $this->request->param(); $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; } elseif (is_dir($inpath . DIRECTORY_SEPARATOR . $val)) { $dirs[] = $val; } else { $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); $filesize .= "M"; } else { $filesize .= "K"; } } else { $filesize .= "B"; } $arr['size'] = $filesize; $filetime = filemtime("$file"); $arr['time'] = date("Y-m-d H:i:s", $filetime); $files[] = $arr; } } $counts = count($dirs) + count($files); $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath); $data = [ 'dirs' => $dirs, 'files' => $files, 'counts' => $counts, 'activeurl' => $activeurl, 'activepath' => $activepath, ]; return $data; } public function delDir() { if ($this->request->isAjax()) { $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; if (count(scandir($dir_name)) > 2) { return ['status' => 1, 'msg' => '不可删除非空目录']; } if (rmdir($dir_name)) { return ['status' => 2, 'msg' => 'success']; } else { return ['status' => 0, 'msg' => 'failed']; } } } public function del() { if ($this->request->isAjax()) { $activepath = $this->request->param('activepath'); $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']; } else { return ['status' => 0, 'msg' => 'failed']; } } } // public function upload() // { // if ($this->request->isGet()) { // return View::fetch()(); // } else { // $isthumb = $this->request->has('isthumb', 'post') ? $this->request->post('isthumb') : 0; // $mode = $this->request->has('formername', 'post') ? $this->request->post('formername') : 0; // $file = request()->file('image'); // $info = $this->saveUpload($file, $mode); // 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) { // $this->error('缩略图生成失败'); // } // } else { // $thumbname = ''; // } // if ($this->request->isAjax()) { // return ['code'=>2, 'picname' => $filename, 'thumbname'=>$thumbname]; // } else { // $this->success('上传成功', '/file_manager/index'); // } // } // } /** * 保存上传的图片 * @param object $file 获取的上传对象 * @param boolean $mode 是否保存原文件名 0(false), 生成新名称; 1(true), 保留原名 * @return object 返回文件保存对象 */ protected function saveUpload($file, $mode) { $validate = ['size' => 2097152, 'ext' => 'jpg,png,gif,jpeg']; if ($mode) { $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads', ''); } else { $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads'); } return $upload; } /** * 站内选择 * @return void */ public function selectPicture() { $data = $this->explorer(); View::assign('data', $data); return View::fetch()(); } public function uploadPicture() { return View::fetch()(); } // public function onlinePicture() // { // if ($this->request->isAjax()) { // $param = $this->request->param(); // $urlimg = $param['filename']; // $download = $param['download']; // $isthumb = $param['isthumb']; // $formername = $param['formername']; // $res = \mylib\GetImageByurl::isImg($urlimg); // if (!$res) { // $this->error('该图片不合法'); // } else { // if (!$download) { // return ['code'=>2,'filename'=>$urlimg]; // } else { // $basepath = \config('url_domain_root') . '/uploads'; // // var_dump($param); // if ($isthumb!=1 && preg_match("#" . $basepath . "#i", $urlimg)) { // $this->error('图片已在服务其中, 可直接选用'); // } // // 按文件夹日期夹存放图片 // $today = date('Ymd', time()); // $savePath = $this->img_path . $today; // if (!is_dir($savePath)) { // if (mkdir($savePath) == false) { // $this->error('下载失败, 请稍后重试'); // } // } // if ($formername==1) { // // 获取原文件名 // $fileinfo = pathinfo($urlimg); // if (!isset($fileinfo['extension']) || !in_array($fileinfo['extension'], ['jpg', 'jpeg', 'png', 'gif'])) { // $ext = \mylib\GetImageByurl::getEXTENSION($urlimg); // } // $filename = $fileinfo['basename'] . '.' . $ext; // } 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) { // $this->error('缩略图生成失败'); // } // } else { // $thumbname = ''; // } // return ['code'=>2, 'filename' => $today . DIRECTORY_SEPARATOR . $filename, 'thumbname'=>$thumbname]; // } else { // $this->error('图片下载失败'); // } // } // } // } else { // return View::fetch()(); // } // } /** * 判断(远程)文件是否为图片 */ // public function isImg() // { // if ($this->request->isAjax()) { // $filename = $this->request->param('filename'); // $res = \mylib\GetImageByurl::isImg($filename); // if (!$res) { // $this->error('该图片不合法'); // } else { // return ['code'=>2,'msg'=>'图片地址可用']; // } // } // } /** * ckeditor 富文本编辑器上传图片 */ public function ckeditorUploadImage() { if ($this->request->isPost()) { $file = $this->request->file('upload'); if ($file) { try { validate( [ 'file' => [ // 限制文件大小(单位b),这里限制为4M 'fileSize' => 4 * 1024 * 1024, // 限制文件后缀,多个后缀以英文逗号分割 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif' ] ], [ 'file.fileSize' => '文件太大', 'file.fileExt' => '不支持的文件后缀', ] )->check(['file' => $file]); $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file); return json([ 'uploaded' => 1, 'fileName' => basename($savename), 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename ]); } catch (\think\exception\ValidateException $e) { $this->error($e->getMessage()); return json([ 'uploaded' => 1, 'error' => ['message'=>$e->getMessage()] ]); } } else { return json([ 'uploaded' => 1, 'error' => ['message'=>'图片不能为空'] ]); } } } }