BaseUpload.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace qiniu\basic;
  12. use think\facade\Config;
  13. /**
  14. * Class BaseUpload
  15. * @package crmeb\basic
  16. */
  17. abstract class BaseUpload extends BaseStorage
  18. {
  19. /**
  20. * 缩略图
  21. * @var string[]
  22. */
  23. protected $thumb = ['big', 'mid', 'small'];
  24. /**
  25. * 缩略图配置
  26. * @var array
  27. */
  28. protected $thumbConfig = [
  29. 'thumb_big_height' => 700,
  30. 'thumb_big_width' => 700,
  31. 'thumb_mid_height' => 400,
  32. 'thumb_mid_width' => 400,
  33. 'thumb_small_height' => 100,
  34. 'thumb_small_width' => 100,
  35. ];
  36. /**
  37. * 水印配置
  38. * @var array
  39. */
  40. protected $waterConfig = [
  41. 'image_watermark_status' => 0,
  42. 'watermark_type' => 1,
  43. 'watermark_image' => '',
  44. 'watermark_opacity' => 0,
  45. 'watermark_position' => 1,
  46. 'watermark_rotate' => 0,
  47. 'watermark_text' => '',
  48. 'watermark_text_angle' => "",
  49. 'watermark_text_color' => '#000000',
  50. 'watermark_text_size' => '5',
  51. 'watermark_text_font' => '',
  52. 'watermark_x' => 0,
  53. 'watermark_y' => 0
  54. ];
  55. /**
  56. * 图片信息
  57. * @var array
  58. */
  59. protected $fileInfo;
  60. /**
  61. * 下载图片信息
  62. */
  63. protected $downFileInfo;
  64. /**
  65. * 要生成缩略图、水印的图片地址
  66. * @var string
  67. */
  68. protected $filePath;
  69. /**
  70. * 验证配置
  71. * @var string
  72. */
  73. protected $validate;
  74. /**
  75. * 保存路径
  76. * @var string
  77. */
  78. protected $path = '';
  79. /**
  80. * 是否自动裁剪
  81. * @var bool
  82. */
  83. protected $authThumb = false;
  84. protected function initialize(array $config)
  85. {
  86. $this->fileInfo = $this->downFileInfo = new \StdClass();
  87. $this->thumbConfig = array_merge($this->thumbConfig, $config['thumb'] ?? []);
  88. $this->waterConfig = array_merge($this->waterConfig, $config['water'] ?? []);
  89. }
  90. /**
  91. * 设置处理缩略图、水印图片路径
  92. * @param string $filePath
  93. * @return $this
  94. */
  95. public function setFilepath(string $filePath)
  96. {
  97. $this->filePath = substr($filePath, 0, 1) === '.' ? substr($filePath, 1) : $filePath;
  98. return $this;
  99. }
  100. /**
  101. * 是否自动裁剪
  102. * @param bool $auth
  103. * @return $this
  104. */
  105. public function setAuthThumb(bool $auth)
  106. {
  107. $this->authThumb = $auth;
  108. return $this;
  109. }
  110. /**
  111. * 上传文件路径
  112. * @param string $path
  113. * @return $this
  114. */
  115. public function to(string $path)
  116. {
  117. $this->path = $path;
  118. return $this;
  119. }
  120. /**
  121. * 获取文件信息
  122. * @return array
  123. */
  124. public function getFileInfo()
  125. {
  126. return $this->fileInfo;
  127. }
  128. /**
  129. * 检测是否是图片
  130. * @param $filePath
  131. * @return bool
  132. */
  133. protected function checkImage($filePath)
  134. {
  135. //获取图像信息
  136. $info = @getimagesize($filePath);
  137. //检测图像合法性
  138. if (false === $info || IMAGETYPE_GIF === $info[2] || empty($info['bits'])) {
  139. return false;
  140. }
  141. return true;
  142. }
  143. /**
  144. * 验证合法上传域名
  145. * @param string $url
  146. * @return string
  147. */
  148. protected function checkUploadUrl(string $url)
  149. {
  150. if ($url && strstr($url, 'http') === false) {
  151. $url = 'http://' . $url;
  152. }
  153. return $url;
  154. }
  155. /**
  156. * 获取系统配置
  157. * @return mixed
  158. */
  159. protected function getConfig()
  160. {
  161. $config = Config::get($this->configFile . '.stores.' . $this->name, []);
  162. if (empty($config)) {
  163. $config['filesize'] = Config::get($this->configFile . '.filesize', []);
  164. $config['fileExt'] = Config::get($this->configFile . '.fileExt', []);
  165. }
  166. return $config;
  167. }
  168. /**
  169. * 设置验证规则
  170. * @param array|null $validate
  171. * @return $this
  172. */
  173. public function validate(?array $validate = null)
  174. {
  175. if (is_null($validate)) {
  176. $validate = $this->getConfig();
  177. }
  178. $this->extractValidate($validate);
  179. return $this;
  180. }
  181. /**
  182. * 验证目录是否正确
  183. * @param string $key
  184. * @return false|string
  185. */
  186. protected function getUploadPath(string $key)
  187. {
  188. $path = ($this->path ? $this->path . '/' : '') . $key;
  189. if ($path && $path[0] === '/') {
  190. $path = substr($path, 1);
  191. }
  192. return $path;
  193. }
  194. /**
  195. * 提取上传验证
  196. */
  197. protected function extractValidate(array $validateArray)
  198. {
  199. $validate = [];
  200. foreach ($validateArray as $key => $value) {
  201. $validate[] = $key . ':' . (is_array($value) ? implode(',', $value) : $value);
  202. }
  203. $this->validate = implode('|', $validate);
  204. unset($validate);
  205. }
  206. /**
  207. * 提取文件名
  208. * @param string $path
  209. * @param string $ext
  210. * @return string
  211. */
  212. protected function saveFileName(string $path = null, string $ext = 'jpg')
  213. {
  214. mt_srand();
  215. return ($path ? substr(md5($path), 0, 5) : '') . date('YmdHis') . rand(0, 9999) . '.' . $ext;
  216. }
  217. /**
  218. * 提取文件后缀以及之前部分
  219. * @param string $path
  220. * @return false|string[]
  221. */
  222. protected function getFileName(string $path)
  223. {
  224. $_empty = ['', ''];
  225. if (!$path) return $_empty;
  226. if (strpos($path, '?')) {
  227. $_tarr = explode('?', $path);
  228. $path = trim($_tarr[0]);
  229. }
  230. $arr = explode('.', $path);
  231. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  232. $ext_name = trim($arr[count($arr) - 1]);
  233. $ext_name = !$ext_name ? 'jpg' : $ext_name;
  234. return [explode('.' . $ext_name, $path)[0], $ext_name];
  235. }
  236. /**
  237. * 获取图片地址
  238. * @param string $filePath
  239. * @param bool $is_parse_url
  240. * @return string
  241. */
  242. protected function getFilePath(string $filePath = '', bool $is_parse_url = false)
  243. {
  244. $path = $filePath ? $filePath : $this->filePath;
  245. if ($is_parse_url) {
  246. $data = parse_url($path);
  247. //远程地址处理
  248. if (isset($data['host']) && isset($data['path'])) {
  249. if (file_exists(app()->getRootPath() . 'public' . $data['path'])) {
  250. $path = $data['path'];
  251. }
  252. }
  253. }
  254. return $path;
  255. }
  256. /**
  257. * 文件是否在本地
  258. * @param string $filePath
  259. * @return bool
  260. */
  261. protected function isLocal(string $filePath)
  262. {
  263. $isLocal = false;
  264. $path = $filePath;
  265. $data = parse_url($path);
  266. //远程地址处理
  267. if (isset($data['host']) && isset($data['path'])) {
  268. if (file_exists(app()->getRootPath() . 'public' . $data['path'])) {
  269. $isLocal = true;
  270. }
  271. }
  272. return $isLocal;
  273. }
  274. /**
  275. * 获取文件类型和大小
  276. * @param string $url
  277. * @param bool $isData
  278. * @return array
  279. */
  280. protected function getFileHeaders(string $url, $isData = true)
  281. {
  282. stream_context_set_default(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
  283. $header['size'] = 0;
  284. $header['type'] = 'image/jpeg';
  285. if (!$isData) {
  286. return $header;
  287. }
  288. try {
  289. $headerArray = get_headers(str_replace('\\', '/', $url), true);
  290. if (!isset($headerArray['Content-Length'])) {
  291. $header['size'] = 0;
  292. } else {
  293. if (is_array($headerArray['Content-Length']) && count($headerArray['Content-Length']) == 2) {
  294. $header['size'] = $headerArray['Content-Length'][1];
  295. } else {
  296. $header['size'] = $headerArray['Content-Length'] ?? 0;
  297. }
  298. }
  299. if (!isset($headerArray['Content-Type'])) {
  300. $header['type'] = 'image/jpeg';
  301. } else {
  302. if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) {
  303. $header['type'] = $headerArray['Content-Type'][1];
  304. } else {
  305. $header['type'] = $headerArray['Content-Type'] ?? 'image/jpeg';
  306. }
  307. }
  308. } catch (\Exception $e) {
  309. }
  310. return $header;
  311. }
  312. /**
  313. * 获取上传信息
  314. * @return array
  315. */
  316. public function getUploadInfo()
  317. {
  318. if (isset($this->fileInfo->filePath)) {
  319. if (strstr($this->fileInfo->filePath, 'http') === false) {
  320. $url = request()->domain() . $this->fileInfo->filePath;
  321. } else {
  322. $url = $this->fileInfo->filePath;
  323. }
  324. $headers = $this->getFileHeaders($url);
  325. return [
  326. 'name' => $this->fileInfo->fileName,
  327. 'real_name' => $this->fileInfo->realName ?? '',
  328. 'size' => $headers['size'] ?? 0,
  329. 'type' => $headers['type'] ?? 'image/jpeg',
  330. 'dir' => $this->fileInfo->filePath,
  331. 'thumb_path' => $this->fileInfo->filePath,
  332. 'thumb_path_big' => $this->fileInfo->filePathBig ?? '',
  333. 'thumb_path_mid' => $this->fileInfo->filePathMid ?? '',
  334. 'thumb_path_small' => $this->fileInfo->filePathSmall ?? '',
  335. 'thumb_path_water' => $this->fileInfo->filePathWater ?? '',
  336. 'time' => time(),
  337. ];
  338. } else {
  339. return [];
  340. }
  341. }
  342. /**
  343. * 获取下载信息
  344. * @return array
  345. */
  346. public function getDownloadInfo()
  347. {
  348. if (isset($this->downFileInfo->downloadFilePath)) {
  349. if (strstr($this->downFileInfo->downloadFilePath, 'http') === false) {
  350. $url = request()->domain() . $this->downFileInfo->downloadFilePath;
  351. } else {
  352. $url = $this->downFileInfo->downloadFilePath;
  353. }
  354. $headers = $this->getFileHeaders($url);
  355. return [
  356. 'name' => $this->downFileInfo->downloadFileName,
  357. 'real_name' => $this->downFileInfo->downloadRealName ?? '',
  358. 'size' => $headers['size'] ?? 0,
  359. 'type' => $headers['type'] ?? 'image/jpeg',
  360. 'dir' => $this->downFileInfo->downloadFilePath ?? '',
  361. 'thumb_path' => $this->downFileInfo->downloadFilePath ?? '',
  362. 'time' => time(),
  363. ];
  364. } else {
  365. return [];
  366. }
  367. }
  368. /**
  369. * 文件上传
  370. * @return mixed
  371. */
  372. abstract public function move(string $file = 'file');
  373. /**
  374. * 文件流上传
  375. * @return mixed
  376. */
  377. abstract public function stream(string $fileContent, string $key = null);
  378. /**
  379. * 删除文件
  380. * @return mixed
  381. */
  382. abstract public function delete(string $filePath);
  383. /**
  384. * 实例化上传
  385. * @return mixed
  386. */
  387. abstract protected function app();
  388. /**
  389. * 获取上传密钥
  390. * @return mixed
  391. */
  392. abstract public function getTempKeys();
  393. /**
  394. * 获取缩略图
  395. * @return mixed
  396. */
  397. abstract public function thumb(string $filePath = '');
  398. /**
  399. * 添加水印
  400. * @return mixed
  401. */
  402. abstract public function water(string $filePath = '');
  403. }