common_upload.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /*
  3. * 常规上传图片公共处理
  4. */
  5. function ds_upload_pic($upload_path, $file_name, $save_name = '', $file_ext = ALLOW_IMG_EXT) {
  6. $file_object = request()->file($file_name);
  7. $disk='local'.rand(0,100);
  8. $file_config = array(
  9. 'disks' => array(
  10. $disk => array(
  11. 'root' => BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . $upload_path
  12. )
  13. )
  14. );
  15. if($file_ext == 'mp4'){
  16. $fileSize = ALLOW_VIDEO_SIZE;
  17. }else{
  18. $fileSize = ALLOW_IMG_SIZE;
  19. }
  20. config($file_config, 'filesystem');
  21. try {
  22. validate(['image' => 'fileSize:' . $fileSize . '|fileExt:' . $file_ext])
  23. ->check(['image' => $file_object]);
  24. if ($save_name) {
  25. $temp = explode('.', $save_name);
  26. if (count($temp) < 2) {
  27. $save_name .= '.png';
  28. }
  29. }
  30. $res=image_filter($_FILES[$file_name]['tmp_name']);
  31. if(!$res['code']){
  32. throw new \think\Exception($res['msg'], 10006);
  33. }
  34. if($res['data']['if_sensitive']){
  35. throw new \think\Exception(implode('、', $res['data']['sensitive_msg']), 10006);
  36. }
  37. $upload_type = config('ds_config.upload_type');
  38. if ($upload_type == 'alioss') {//远程保存
  39. if (!$save_name) {
  40. $save_name = date('YmdHis') . rand(10000, 99999) . '.png';
  41. }
  42. $accessId = config('ds_config.alioss_accessid');
  43. $accessSecret = config('ds_config.alioss_accesssecret');
  44. $bucket = config('ds_config.alioss_bucket');
  45. $endpoint = config('ds_config.alioss_endpoint');
  46. $aliendpoint_type = config('ds_config.aliendpoint_type') == '1' ? true : false;
  47. $object = $upload_path . '/' . 'alioss_' . $save_name;
  48. $filePath = $_FILES[$file_name]['tmp_name'];
  49. require_once root_path() . 'vendor/aliyuncs/oss-sdk-php/autoload.php';
  50. $OssClient = new \OSS\OssClient($accessId, $accessSecret, $endpoint, $aliendpoint_type);
  51. $fileinfo = $OssClient->uploadFile($bucket, $object, $filePath);
  52. $img_path = $fileinfo['info']['url'];
  53. $file_name = substr(strrchr($img_path, "/"), 1);
  54. } else {
  55. if ($save_name) {
  56. $file_name = \think\facade\Filesystem::disk($disk)->putFileAs('', $file_object, $save_name);
  57. } else {
  58. $file_name = \think\facade\Filesystem::disk($disk)->putFile('', $file_object, 'uniqid');
  59. }
  60. }
  61. return ds_callback(true, '', array('file_name' => $file_name));
  62. } catch (\Exception $e) {
  63. return ds_callback(false, $e->getMessage());
  64. }
  65. }
  66. /**
  67. * 取得图片的完整URL路径
  68. *
  69. * @param string $file 视频名称
  70. * @return string
  71. */
  72. function ds_get_pic($upload_path, $file){
  73. $fname = basename($file);
  74. $upload_type=explode('_', $fname);
  75. if(in_array($upload_type['0'], array('alioss', 'cos'))){//对象存储文件
  76. $aliendpoint_type = config('ds_config.aliendpoint_type');
  77. if($aliendpoint_type) {
  78. return HTTP_TYPE.config('ds_config.alioss_endpoint') . '/' . $upload_path . '/' . $file;
  79. }else{
  80. return 'https://'.config('ds_config.alioss_bucket').'.'.config('ds_config.alioss_endpoint') . '/' . $upload_path . '/' . $file;
  81. }
  82. }else{
  83. if ($file && file_exists(BASE_UPLOAD_PATH . '/' . $upload_path . '/' . $file)) {
  84. return UPLOAD_SITE_URL . '/' . $upload_path . '/' . $file;
  85. }else{
  86. return '';
  87. }
  88. }
  89. }
  90. /*
  91. * 公共生成缩略图
  92. * @param string $upload_path 上传文件路径
  93. * @param string $file_name 上传设置的文件名称
  94. * @param array $thumb_width 设置的图片宽度
  95. * @param array $thumb_height 设置的图片高度
  96. * @param array $thumb_ext 为空表示为不生成多余的图片,直接按照比例生成覆盖
  97. * @return string
  98. */
  99. function ds_create_thumb($upload_path, $file_name, $thumb_width, $thumb_height, $thumb_ext = '') {
  100. if (!file_exists($upload_path . '/' . $file_name)) {
  101. return;
  102. }
  103. $thumb_width = explode(',', $thumb_width);
  104. $thumb_height = explode(',', $thumb_height);
  105. if (empty($thumb_ext)) {
  106. //为空则覆盖原有图片
  107. $image = \think\Image::open($upload_path . '/' . $file_name);
  108. $image->thumb($thumb_width[0], $thumb_height[0], \think\Image::THUMB_CENTER)->save($upload_path . '/' . $file_name);
  109. } else {
  110. $common_images_ext = explode(',', COMMON_IMAGES_EXT);
  111. $thumb_ext = explode(',', $thumb_ext);
  112. $ifthumb = FALSE;
  113. if ((count($thumb_width) == count($thumb_height)) && (count($thumb_width) == count($thumb_ext))) {
  114. $ifthumb = TRUE;
  115. }
  116. if ($ifthumb) {
  117. for ($i = 0; $i < count($thumb_width); $i++) {
  118. if (in_array($thumb_ext[$i], $common_images_ext)) {
  119. $image = \think\Image::open($upload_path . '/' . $file_name);
  120. $image->thumb($thumb_width[$i], $thumb_width[$i], \think\Image::THUMB_CENTER)->save($upload_path . '/' . str_ireplace('.', $thumb_ext[$i] . '.', $file_name));
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /*
  127. * 公共删除图片
  128. */
  129. function ds_unlink($upload_path, $file_name) {
  130. $common_images_ext = explode(',', COMMON_IMAGES_EXT);
  131. foreach ($common_images_ext as $ext) {
  132. $thumb_file = str_ireplace('.', $ext . '.', $file_name);
  133. @unlink($upload_path . DIRECTORY_SEPARATOR . $thumb_file);
  134. }
  135. @unlink($upload_path . DIRECTORY_SEPARATOR . $file_name);
  136. }
  137. /**
  138. * 只针对于相册图片上传的图片进行处理
  139. * upload_path 文件保存路径
  140. * file_name 上传文件的value值
  141. * save_name 文件保存名称
  142. */
  143. function upload_albumpic($upload_path, $file_name = 'file', $save_name) {
  144. //判断是否上传图片
  145. if (!empty($_FILES[$file_name]['name'])) {
  146. $res=image_filter($_FILES[$file_name]['tmp_name']);
  147. if(!$res['code']){
  148. return array('code' => '10001', 'message' => $res['msg'], 'result' => '');
  149. }
  150. if($res['data']['if_sensitive']){
  151. return array('code' => '10001', 'message' => implode('、', $res['data']['sensitive_msg']), 'result' => '');
  152. }
  153. $upload_type = config('ds_config.upload_type');
  154. //远程保存
  155. if ($upload_type == 'alioss') {
  156. $accessId = config('ds_config.alioss_accessid');
  157. $accessSecret = config('ds_config.alioss_accesssecret');
  158. $bucket = config('ds_config.alioss_bucket');
  159. $endpoint = config('ds_config.alioss_endpoint');
  160. $aliendpoint_type = config('ds_config.aliendpoint_type') == '1' ? true : false;
  161. if (!strpos($save_name, '.')) {
  162. $save_name .= '.' . pathinfo($_FILES[$file_name]['name'], PATHINFO_EXTENSION);
  163. }
  164. $object = $upload_path . '/' . 'alioss_' . $save_name;
  165. $filePath = $_FILES[$file_name]['tmp_name'];
  166. require_once root_path() . 'vendor/aliyuncs/oss-sdk-php/autoload.php';
  167. $OssClient = new \OSS\OssClient($accessId, $accessSecret, $endpoint, $aliendpoint_type);
  168. try {
  169. $fileinfo = $OssClient->uploadFile($bucket, $object, $filePath);
  170. return array('code' => '10000', 'message' => '', 'result' => $fileinfo['info']['url']);
  171. } catch (OssException $e) {
  172. return array('code' => '10001', 'message' => $e->getMessage(), 'result' => '');
  173. }
  174. } elseif ($upload_type == 'local') {
  175. //本地图片保存
  176. $file_object = request()->file($file_name);
  177. $upload_path = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . $upload_path;
  178. $file_config = array(
  179. 'disks' => array(
  180. 'local' => array(
  181. 'root' => $upload_path
  182. )
  183. )
  184. );
  185. config($file_config, 'filesystem');
  186. try {
  187. $temp = explode('.', $save_name);
  188. if (count($temp) < 2) {
  189. $save_name .= '.png';
  190. }
  191. validate(['image' => 'fileSize:' . ALLOW_IMG_SIZE . '|fileExt:' . ALLOW_IMG_EXT])
  192. ->check(['image' => $file_object]);
  193. $file_name = \think\facade\Filesystem::putFileAs('', $file_object, $save_name);
  194. $img_path = $upload_path . '/' . $file_name;
  195. create_albumpic_thumb($upload_path, $file_name);
  196. return array('code' => '10000', 'message' => '', 'result' => $img_path);
  197. } catch (\Exception $e) {
  198. $error = $e->getMessage();
  199. $data['code'] = '10001';
  200. $data['message'] = $error;
  201. $data['result'] = $_FILES[$file_name]['name'];
  202. return $data;
  203. }
  204. }
  205. //预留文件类型检测
  206. } else {
  207. return array('code' => '10001', 'message' => '', 'result' => '');
  208. }
  209. }
  210. /*
  211. * 生成相册图片的缩略图
  212. * upload_path 文件路径
  213. * file_name 文件名称
  214. */
  215. function create_albumpic_thumb($upload_path, $file_name) {
  216. if (!file_exists($upload_path . '/' . $file_name)) {
  217. return;
  218. }
  219. $ifthumb = FALSE;
  220. if (defined('GOODS_IMAGES_WIDTH') && defined('GOODS_IMAGES_HEIGHT') && defined('GOODS_IMAGES_EXT')) {
  221. $thumb_width = explode(',', GOODS_IMAGES_WIDTH);
  222. $thumb_height = explode(',', GOODS_IMAGES_HEIGHT);
  223. $thumb_ext = explode(',', GOODS_IMAGES_EXT);
  224. if (count($thumb_width) == count($thumb_height) && count($thumb_width) == count($thumb_ext)) {
  225. $ifthumb = TRUE;
  226. }
  227. }
  228. if ($ifthumb) {
  229. for ($i = 0; $i < count($thumb_width); $i++) {
  230. $image = \think\Image::open($upload_path . '/' . $file_name);
  231. $image->thumb($thumb_width[$i], $thumb_height[$i], \think\Image::THUMB_CENTER)->save($upload_path . '/' . str_ireplace('.', $thumb_ext[$i] . '.', $file_name));
  232. }
  233. }
  234. }
  235. /* * 删除商品图文件
  236. * pic_list 要删除的文件
  237. * */
  238. function del_albumpic($pic_list) {
  239. if (!empty($pic_list) && is_array($pic_list)) {
  240. $count = '0';
  241. foreach ($pic_list as $val) {
  242. $upload_type = explode('_', $val['apic_cover']);
  243. if ($upload_type['0'] == 'alioss') {
  244. $count++;
  245. }
  246. }
  247. $image_ext = explode(',', GOODS_IMAGES_EXT);
  248. foreach ($pic_list as $v) {
  249. $upload_type = explode('_', $v['apic_cover']);
  250. //外网存储图片
  251. if (in_array($upload_type['0'], array('alioss', 'cos'))) {
  252. if ($upload_type['0'] == 'alioss') {
  253. if ($count > 1) {
  254. $object[] = ATTACH_GOODS . '/' . $v['store_id'] . '/' . date('Ymd',$v['apic_uploadtime']) . '/' . $v['apic_cover'];
  255. } else {
  256. $object = ATTACH_GOODS . '/' . $v['store_id'] . '/' . date('Ymd',$v['apic_uploadtime']) . '/' . $v['apic_cover'];
  257. }
  258. }
  259. } else {
  260. $upload_path = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_GOODS . DIRECTORY_SEPARATOR . $v['store_id'] . DIRECTORY_SEPARATOR . date('Ymd',$v['apic_uploadtime']);
  261. foreach ($image_ext as $ext) {
  262. $file = str_ireplace('.', $ext . '.', $v['apic_cover']);
  263. @unlink($upload_path . DIRECTORY_SEPARATOR . $file);
  264. }
  265. @unlink($upload_path . DIRECTORY_SEPARATOR . $v['apic_cover']);
  266. }
  267. }
  268. $upload_type = config('ds_config.upload_type');
  269. if ($upload_type == 'alioss') {
  270. //外网存储图片删除
  271. $accessId = config('ds_config.alioss_accessid');
  272. $accessSecret = config('ds_config.alioss_accesssecret');
  273. $bucket = config('ds_config.alioss_bucket');
  274. $endpoint = config('ds_config.alioss_endpoint');
  275. $aliendpoint_type = config('ds_config.aliendpoint_type') == '1' ? true : false;
  276. require_once root_path() . 'vendor/aliyuncs/oss-sdk-php/autoload.php';
  277. $OssClient = new \OSS\OssClient($accessId, $accessSecret, $endpoint, $aliendpoint_type);
  278. try {
  279. if (is_array($object)) {
  280. $OssClient->deleteObjects($bucket, $object);
  281. } else {
  282. $OssClient->deleteObject($bucket, $object);
  283. }
  284. return array('code' => '10000', 'message' => '', 'result' => '');
  285. } catch (OssException $e) {
  286. return array('code' => '10001', 'message' => $e->getMessage(), 'result' => '');
  287. }
  288. }
  289. return array('code' => '10001', 'message' => '', 'result' => '');
  290. }
  291. }
  292. /**
  293. * 获取图片的Base64编码(不支持url)
  294. *
  295. * @param $img_file 传入本地图片地址
  296. *
  297. * @return string
  298. */
  299. function imgToBase64($img_file) {
  300. $data=array();
  301. $img_base64 = '';
  302. if (file_exists($img_file)) {
  303. $app_img_file = $img_file; // 图片路径
  304. $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
  305. //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
  306. $fp = fopen($app_img_file, "r"); // 图片是否可读权限
  307. if ($fp) {
  308. $filesize = filesize($app_img_file);
  309. $content = fread($fp, $filesize);
  310. $file_content = base64_encode($content); // base64编码
  311. switch ($img_info[2]) { //判读图片类型
  312. case 1: $img_type = "gif";
  313. break;
  314. case 2: $img_type = "jpg";
  315. break;
  316. case 3: $img_type = "png";
  317. break;
  318. }
  319. $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
  320. }
  321. fclose($fp);
  322. $data=array(
  323. 'type'=>$img_type,
  324. 'content'=>$file_content,
  325. 'result'=>$img_base64
  326. );
  327. }
  328. return $data; //返回图片的base64
  329. }