Upload.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\shop\controller;
  11. use app\model\upload\Upload as UploadModel;
  12. use app\model\upload\Config as ConfigModel;
  13. use app\model\upload\Album as AlbumModel;
  14. use think\Exception;
  15. use app\model\BaseModel;
  16. /**
  17. * 图片上传
  18. * Class Verify
  19. * @package app\shop\controller
  20. */
  21. class Upload extends BaseShop
  22. {
  23. public $site_id = 0;
  24. protected $app_module = "shop";
  25. public function __construct()
  26. {
  27. //执行父类构造函数
  28. parent::__construct();
  29. $this->site_id = request()->siteid();
  30. if (empty($this->site_id)) {
  31. $this->site_id = input('site_id', 0);
  32. request()->siteid($this->site_id);
  33. }
  34. }
  35. /**
  36. * 上传配置
  37. */
  38. public function config()
  39. {
  40. $config_model = new ConfigModel();
  41. if (request()->isAjax()) {
  42. //基础上传
  43. $max_filesize = input('max_filesize', '10240');//允许上传大小 默认kb
  44. $compress = trim(input('compress', ''));//图片压缩
  45. /*************************************************************************** 缩略图 *******************************************************************/
  46. $thumb_big_width = input('thumb_big_width', 400);//缩略大图 宽
  47. $thumb_big_height = input('thumb_big_height', 400);//缩略大图 高
  48. $thumb_mid_width = input('thumb_mid_width', 200);//缩略中图 宽
  49. $thumb_mid_height = input('thumb_mid_height', 200);//缩略中图 高
  50. $thumb_small_width = input('thumb_small_width', 100);//缩略小图 宽
  51. $thumb_small_height = input('thumb_small_height', 100);//缩略小图 高
  52. /*************************************************************************** 水印 *******************************************************************/
  53. $is_watermark = input('is_watermark', 0);//是否开启水印
  54. $watermark_type = input('watermark_type', '1');//水印类型 1图片 2文字
  55. $watermark_source = input('watermark_source', '');//水印图片来源
  56. $watermark_position = input('watermark_position', 'top-left');//水印图片位置(相对于当前图像)top-left(默认)top top-right left center right bottom-left bottom bottom-right
  57. $watermark_x = input('watermark_x', 0);//水印图片横坐标偏移量
  58. $watermark_y = input('watermark_y', 0);//水印图片纵坐标偏移量
  59. $watermark_opacity = input('watermark_opacity', '0');//水印图片透明度
  60. $watermark_rotate = input('watermark_rotate', '0');//水印图片倾斜度
  61. $watermark_text = input('watermark_text', '');//水印文字
  62. $watermark_text_file = input('watermark_text_file', '');//水印文字 字体文件。设置True Type Font文件的路径,或者GD库内部字体之一的1到5之间的整数值。 默认值:1
  63. $watermark_text_size = input('watermark_text_size', '12');//水印文字 字体大小。字体大小仅在设置字体文件时可用,否则将被忽略。 默认值:12
  64. $watermark_text_color = input('watermark_text_color', '#000000');//水印文字 字体颜色
  65. $watermark_text_align = input('watermark_text_align', 'left');//水印文字水平对齐方式 水平对齐方式:left,right,center。默认left
  66. $watermark_text_valign = input('watermark_text_valign', 'bottom');//水印文字垂直对齐方式 垂直对齐方式:top,bottom,middle。默认bottom
  67. $watermark_text_angle = input('watermark_text_angle', '0');//文本旋转角度。文本将围绕垂直和水平对齐点逆时针旋转。 旋转仅在设置字体文件时可用,否则将被忽略
  68. $data = array(
  69. //上传相关配置
  70. 'upload' => array(
  71. 'max_filesize' => $max_filesize * 1024,//最大上传限制,
  72. 'compress' => $compress
  73. ),
  74. //缩略图相关配置
  75. 'thumb' => array(
  76. 'thumb_big_width' => $thumb_big_width,
  77. 'thumb_big_height' => $thumb_big_height,
  78. 'thumb_mid_width' => $thumb_mid_width,
  79. 'thumb_mid_height' => $thumb_mid_height,
  80. 'thumb_small_width' => $thumb_small_width,
  81. 'thumb_small_height' => $thumb_small_height,
  82. ),
  83. //水印相关配置
  84. 'water' => array(
  85. 'is_watermark' => $is_watermark,
  86. 'watermark_type' => $watermark_type,
  87. 'watermark_source' => $watermark_source,
  88. 'watermark_position' => $watermark_position,
  89. 'watermark_x' => $watermark_x,
  90. 'watermark_y' => $watermark_y,
  91. 'watermark_opacity' => $watermark_opacity,
  92. 'watermark_rotate' => $watermark_rotate,
  93. 'watermark_text' => $watermark_text,
  94. 'watermark_text_file' => $watermark_text_file,
  95. 'watermark_text_size' => $watermark_text_size,
  96. 'watermark_text_color' => $watermark_text_color,
  97. 'watermark_text_align' => $watermark_text_align,
  98. 'watermark_text_valign' => $watermark_text_valign,
  99. 'watermark_text_angle' => $watermark_text_angle,
  100. ),
  101. );
  102. $this->addLog('修改上传配置');
  103. $result = $config_model->setUploadConfig($data);
  104. return $result;
  105. } else {
  106. $this->forthMenu();
  107. $config_result = $config_model->getUploadConfig();
  108. $config = $config_result['data'];
  109. $config['value']['upload']['max_filesize'] = $config['value']['upload']['max_filesize'] / 1024;
  110. $this->assign('config', $config);
  111. //图片水印位置
  112. $position = array(
  113. 'top-left' => '上左',
  114. 'top' => '上中',
  115. 'top-right' => '上右',
  116. 'left' => '左',
  117. 'center' => '中',
  118. 'right' => '右',
  119. 'bottom-left' => '下左',
  120. 'bottom' => '下中',
  121. 'bottom-right' => '下右',
  122. );
  123. $this->assign('position', $position);
  124. //文字水印位置
  125. $text_position = array(
  126. 'top-left' => '上左',
  127. 'top-center' => '上中',
  128. 'top-right' => '上右',
  129. 'middle-left' => '左',
  130. 'middle-center' => '中',
  131. 'middle-right' => '右',
  132. 'bottom-left' => '下左',
  133. 'bottom-center' => '下中',
  134. 'bottom-right' => '下右',
  135. );
  136. $this->assign('text_position', $text_position);
  137. return $this->fetch('upload/config');
  138. }
  139. }
  140. /**
  141. * 云上传方式
  142. */
  143. public function oss()
  144. {
  145. if (request()->isAjax()) {
  146. $config_model = new ConfigModel();
  147. $list = event('OssType', []);
  148. return $config_model->success($list);
  149. } else {
  150. $this->forthMenu();
  151. return $this->fetch('upload/oss');
  152. }
  153. }
  154. /**
  155. * 上传(不存入相册)
  156. * @return \app\model\upload\Ambigous|\multitype
  157. */
  158. public function image()
  159. {
  160. $upload_model = new UploadModel($this->site_id, $this->app_module);
  161. $thumb_type = input('thumb', '');
  162. $name = input('name', '');
  163. $width = input('width', '');
  164. $height = input('height', '');
  165. $watermark = input('watermark', 0); // 是否需生成水印
  166. $cloud = input('cloud', 1); // 是否需上传到云存储
  167. $param = array(
  168. 'thumb_type' => '',
  169. 'name' => 'file',
  170. 'watermark' => $watermark,
  171. 'cloud' => $cloud,
  172. 'width' => $width,
  173. 'height' => $height
  174. );
  175. $path = $this->site_id > 0 ? 'common/images/' . date('Ymd') . '/' : 'common/images/' . date('Ymd') . '/';
  176. $result = $upload_model->setPath($path)->image($param);
  177. return $result;
  178. }
  179. /**
  180. * 上传 存入相册
  181. * @return \multitype
  182. */
  183. public function album()
  184. {
  185. $upload_model = new UploadModel($this->site_id);
  186. $album_id = input('album_id', 0);
  187. $name = input('name', '');
  188. $is_thumb = input('is_thumb',0);
  189. $param = array(
  190. 'thumb_type' => ['BIG', 'MID', 'SMALL'],
  191. 'name' => 'file',
  192. 'album_id' => $album_id,
  193. 'is_thumb' => $is_thumb
  194. );
  195. $result = $upload_model->setPath('common/images/' . date('Ymd') . '/')->imageToAlbum($param);
  196. return $result;
  197. }
  198. /**
  199. * 视频上传到素材
  200. * @return \multitype
  201. */
  202. public function videoToAlbum()
  203. {
  204. $upload_model = new UploadModel($this->site_id);
  205. $name = input('name', '');
  206. $album_id = input('album_id', 0);
  207. $param = array(
  208. 'name' => 'file',
  209. 'album_id' => $album_id,
  210. );
  211. $result = $upload_model->setPath('common/video/' . date('Ymd') . '/')->videoToAlbum($param);
  212. return $result;
  213. }
  214. /**
  215. * 视频上传
  216. * @return \multitype
  217. */
  218. public function video()
  219. {
  220. $upload_model = new UploadModel($this->site_id);
  221. $name = input('name', '');
  222. $param = array(
  223. 'name' => 'file'
  224. );
  225. $result = $upload_model->setPath('common/video/' . date('Ymd') . '/')->video($param);
  226. return $result;
  227. }
  228. /*
  229. * 替换视频文件
  230. * */
  231. public function modifyVideoFile()
  232. {
  233. // 实例化响应数据结构生成类
  234. $base_model = new BaseModel();
  235. try {
  236. // 参数
  237. $album_id = input('album_id', '');
  238. $pic_id = input('pic_id', '');
  239. // 获取视频信息
  240. $album_model = new AlbumModel($this->site_id);
  241. $get_pic_info = array(
  242. ['pic_id', '=', $pic_id],
  243. ['site_id', '=', $this->site_id],
  244. );
  245. // 视频信息
  246. $pic_info = $album_model->getAlbumPicInfo($get_pic_info);
  247. // 判断是否找到有效视频
  248. if (empty($pic_info) || empty($pic_info['data'])) {
  249. return json($base_model->error('', 'FAIL'));
  250. }
  251. // 实例化文件上传类
  252. $upload_model = new UploadModel($this->site_id);
  253. $upload_param = array(
  254. 'name' => 'file',
  255. 'album_id' => $album_id,
  256. 'pic_id' => $pic_id,
  257. );
  258. $result = $upload_model->setPath('common/video/' . date('Ymd') . '/')->modifyVideoFile($upload_param);
  259. return json($result);
  260. } catch ( \Exception $e ) {
  261. return json($base_model->error($e, 'FAIL'));
  262. }
  263. }
  264. /**
  265. * 上传(不存入相册)
  266. * @return \app\model\upload\Ambigous|\multitype
  267. */
  268. public function upload()
  269. {
  270. $upload_model = new UploadModel();
  271. $thumb_type = input('thumb', '');
  272. $name = input('name', '');
  273. $param = array(
  274. 'thumb_type' => '',
  275. 'name' => 'file'
  276. );
  277. $result = $upload_model->setPath('common/images/' . date('Ymd') . '/')->image($param);
  278. return $result;
  279. }
  280. /**
  281. * 校验文件
  282. */
  283. public function checkfile()
  284. {
  285. $upload_model = new UploadModel();
  286. $result = $upload_model->domainCheckFile(['name' => 'file']);
  287. return $result;
  288. }
  289. /**
  290. * 上传文件
  291. */
  292. public function file()
  293. {
  294. $upload_model = new UploadModel($this->site_id);
  295. $param = array(
  296. 'name' => 'file',
  297. 'extend_type' => ['xlsx']
  298. );
  299. $result = $upload_model->setPath('common/file/' . date('Ymd') . '/')->file($param);
  300. return $result;
  301. }
  302. /**
  303. * 删除文件
  304. */
  305. public function deleteFile()
  306. {
  307. if (request()->isAjax()) {
  308. $path = input('path', '');
  309. $res = false;
  310. if (!empty($path)) {
  311. $res = delFile($path);
  312. }
  313. return $res;
  314. }
  315. }
  316. /*
  317. * 替换图片文件
  318. * */
  319. public function modifyFile()
  320. {
  321. // 实例化响应数据结构生成类
  322. $base_model = new BaseModel();
  323. try {
  324. // 参数
  325. $album_id = input('album_id', '');
  326. $pic_id = input('pic_id', '');
  327. // 获取图片信息
  328. $album_model = new AlbumModel($this->site_id);
  329. $get_pic_info = array(
  330. ['pic_id', '=', $pic_id],
  331. ['site_id', '=', $this->site_id],
  332. );
  333. // 图片信息
  334. $pic_info = $album_model->getAlbumPicInfo($get_pic_info);
  335. // 判断是否找到有效图片
  336. if (empty($pic_info) || empty($pic_info['data'])) {
  337. return json($base_model->error('', 'FAIL'));
  338. }
  339. $file_full_name = basename($pic_info['data']['pic_path']);
  340. $pic_path = str_replace($file_full_name, '', $pic_info['data']['pic_path']);
  341. $pic_path = str_replace('upload/1/', '', $pic_path);
  342. // 文件名及后缀
  343. $filename_arr = explode('.', $file_full_name);
  344. $filename = $filename_arr[0];
  345. $suffix = $filename_arr[1];
  346. // 实例化文件上传类
  347. $upload_model = new UploadModel($this->site_id);
  348. $upload_param = array(
  349. 'name' => 'file',
  350. 'album_id' => $album_id,
  351. 'pic_id' => $pic_id,
  352. 'thumb_type' => ['BIG', 'MID', 'SMALL'],
  353. 'filename' => $filename,
  354. 'suffix' => $suffix
  355. );
  356. $parse_res = parse_url($pic_path);
  357. $pic_path = ltrim($parse_res['path'], '/');
  358. $result = $upload_model->setPath($pic_path)->modifyFile($upload_param);
  359. return json($result);
  360. } catch ( \Exception $e ) {
  361. return json($base_model->error($e, 'FAIL'));
  362. }
  363. }
  364. /**
  365. * 下载图片
  366. */
  367. public function download()
  368. {
  369. $filename = input('img_url', '');
  370. $filename = urldecode($filename);
  371. // 文件类型,作为头部发送给浏览器
  372. // $type = filetype($filename);
  373. $type = pathinfo($filename)['extension'];
  374. if ($type=='png'||$type=='jpg'){
  375. // 发送文件头部
  376. header("Content-type: $type");
  377. header("Content-Disposition: attachment;filename=$filename");
  378. header("Content-Transfer-Encoding: binary");
  379. header('Pragma: no-cache');
  380. header('Expires: 0');
  381. // 发送文件内容
  382. set_time_limit(0);
  383. readfile($filename);
  384. }else{
  385. $this->error();
  386. }
  387. }
  388. }