Ckeditor.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * | Ueditor编辑器后台控制器制器
  6. * +----------------------------------------------------------------------
  7. */
  8. namespace app\sys\controller;
  9. use think\App;
  10. use think\facade\Config;
  11. class Ckeditor extends Base
  12. {
  13. /**
  14. * 图片上传
  15. * @file upload_file 上传的文件
  16. * @param string $img_id 图片ipnut text id 默认值 picture
  17. * @param boolean $water 是否添加水印
  18. * @param boolean $thumb 是否制作缩略图
  19. * @param int $width 缩略图最大宽
  20. * @param int $height 缩略图最大高
  21. * @param bool $overwrite 生成缩略图后是否保存原图
  22. */
  23. public function uploadimg()
  24. {
  25. if ($this->request->isPost()) {
  26. $file = $this->request->file('upload');
  27. if ($file) {
  28. try {
  29. validate(
  30. [
  31. 'file' => [
  32. // 限制文件大小(单位b),这里限制为4M
  33. 'fileSize' => 4 * 1024 * 1024,
  34. // 限制文件后缀,多个后缀以英文逗号分割
  35. 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
  36. ]
  37. ],
  38. [
  39. 'file.fileSize' => '文件太大',
  40. 'file.fileExt' => '不支持的文件后缀',
  41. ]
  42. )->check(['file' => $file]);
  43. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  44. return json([
  45. 'uploaded' => 1,
  46. 'fileName' => basename($savename),
  47. 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
  48. ]);
  49. } catch (\think\exception\ValidateException $e) {
  50. $this->error($e->getMessage());
  51. return json([
  52. 'uploaded' => 1,
  53. 'error' => ['message'=>$e->getMessage()]
  54. ]);
  55. }
  56. } else {
  57. return json([
  58. 'uploaded' => 1,
  59. 'error' => ['message'=>'图片不能为空']
  60. ]);
  61. }
  62. }
  63. }
  64. }