Index.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\cropper\controller;
  3. use think\addons\Controller;
  4. use think\Config;
  5. use think\Hook;
  6. /**
  7. * 图片裁剪
  8. *
  9. */
  10. class Index extends Controller
  11. {
  12. protected $model = null;
  13. public function _initialize()
  14. {
  15. // 语言检测
  16. $lang = $this->request->langset();
  17. $lang = preg_match("/^([a-zA-Z0-9\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  18. $site = Config::get("site");
  19. $upload = \app\common\model\Config::upload();
  20. // 上传信息配置后
  21. Hook::listen("upload_config_init", $upload);
  22. // 配置信息
  23. $config = [
  24. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  25. 'upload' => $upload,
  26. 'modulename' => 'addons',
  27. 'controllername' => 'index',
  28. 'actionname' => $this->request->action(),
  29. 'jsname' => 'cropper',
  30. 'moduleurl' => rtrim(url("/index", '', false), '/'),
  31. 'language' => $lang
  32. ];
  33. $config = array_merge($config, Config::get("view_replace_str"));
  34. Config::set('upload', array_merge(Config::get('upload'), $upload));
  35. // 配置信息后
  36. Hook::listen("config_init", $config);
  37. $this->view->assign('jsconfig', $config);
  38. $this->view->assign('site', $site);
  39. parent::_initialize();
  40. }
  41. public function index()
  42. {
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 图片剪裁
  47. */
  48. public function cropper()
  49. {
  50. $config = get_addon_config('cropper');
  51. $get = $this->request->get();
  52. $allowAttr = [
  53. 'aspectRatio',
  54. 'autoCropArea',
  55. 'cropBoxMovable',
  56. 'cropBoxResizable',
  57. 'minCropBoxWidth',
  58. 'minCropBoxHeight',
  59. 'minContainerWidth',
  60. 'minContainerHeight',
  61. 'minCanvasHeight',
  62. 'minCanvasWidth',
  63. 'croppedWidth',
  64. 'croppedHeight',
  65. 'croppedMinWidth',
  66. 'croppedMinHeight',
  67. 'croppedMaxWidth',
  68. 'croppedMaxHeight',
  69. 'containerMinHeight',
  70. 'containerMaxHeight',
  71. 'customWidthHeight',
  72. 'customAspectRatio'
  73. ];
  74. $attr = array_intersect_key($get, array_flip($allowAttr));
  75. foreach ($attr as $index => &$item) {
  76. $item = floatval($item);
  77. }
  78. $config = array_merge($config, $attr, ['url' => $get['url'] ?? '', 'fillColor' => $get['fillColor'] ?? '']);
  79. $config['fillColor'] = $config['fillColor'] && $config['fillColor'] !== 'transparent' ? '#' . ltrim($config['fillColor'], '#') : 'transparent';
  80. $this->view->assign("cropper", $config);
  81. return $this->view->fetch();
  82. }
  83. }