You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
8.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Http\Request;
  4. use App\Common\ReturnData;
  5. use App\Common\Helper;
  6. class ImageController extends BaseController
  7. {
  8. public $path;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->path = '/uploads/'.date('Y/m',time());
  13. }
  14. //阿里云OSS图片上传
  15. public function ossImageUpload()
  16. {
  17. $res = $this->aliyunOSSFileUpload($_FILES);
  18. if($res['code'] == 1)
  19. {
  20. $this->success($res['data']);
  21. }
  22. $this->error($res['msg']);
  23. }
  24. /**
  25. * 多文件上传,成功返回路径,不含域名
  26. * 格式1:
  27. * <input type="file" name="file1">
  28. * <input type="file" name="file2">
  29. * <input type="file" name="file3">
  30. * 格式2:
  31. * <input type="file" name="file[]">
  32. * <input type="file" name="file[]">
  33. * <input type="file" name="file[]">
  34. */
  35. public function imageUpload(Request $request)
  36. {
  37. $res = [];
  38. $files = $_FILES;//得到传输的数据
  39. if($files)
  40. {
  41. // 对上传文件数组信息处理
  42. $files = $this->dealFiles($files);
  43. foreach($files as $key=>$file)
  44. {
  45. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  46. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  47. $uploads_path = $this->path; //存储路径
  48. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  49. //判断文件类型是否被允许上传
  50. if(!in_array($type, $allow_type))
  51. {
  52. //如果不被允许,则直接停止程序运行
  53. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  54. }
  55. //判断是否是通过HTTP POST上传的
  56. if(!is_uploaded_file($file['tmp_name']))
  57. {
  58. //如果不是通过HTTP POST上传的
  59. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  60. }
  61. //文件小于1M
  62. if ($file["size"] < 2048000)
  63. {
  64. if ($file["error"] > 0)
  65. {
  66. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
  67. }
  68. else
  69. {
  70. if(!file_exists(base_path('public').$uploads_path))
  71. {
  72. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  73. }
  74. move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
  75. }
  76. }
  77. else
  78. {
  79. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  80. }
  81. $res[] = $image_path;
  82. }
  83. }
  84. return ReturnData::create(ReturnData::SUCCESS,$res);
  85. }
  86. public function aliyunOSSFileUpload($files)
  87. {
  88. $res = [];
  89. //$files = $_FILES;//得到传输的数据
  90. $path = 'data/uploads/'.date('Y/m',time());
  91. if($files)
  92. {
  93. // 对上传文件数组信息处理
  94. $files = $this->dealFiles($files);
  95. foreach($files as $key=>$file)
  96. {
  97. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  98. $image_path = $path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  99. $uploads_path = $path; //存储路径
  100. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  101. //判断文件类型是否被允许上传
  102. if(!in_array($type, $allow_type))
  103. {
  104. //如果不被允许,则直接停止程序运行
  105. //$this->error('文件格式不正确');
  106. return ['code'=>0,'msg'=>'文件格式不正确','data'=>''];
  107. }
  108. //判断是否是通过HTTP POST上传的
  109. if(!is_uploaded_file($file['tmp_name']))
  110. {
  111. //如果不是通过HTTP POST上传的
  112. //$this->error('上传失败');
  113. return ['code'=>0,'msg'=>'上传失败','data'=>''];
  114. }
  115. //文件小于1M
  116. if ($file["size"] < 2048000)
  117. {
  118. if ($file["error"] > 0)
  119. {
  120. //$this->error($file["error"]);
  121. return ['code'=>0,'msg'=>$file["error"],'data'=>''];
  122. }
  123. else
  124. {
  125. /* if(!file_exists(substr(ROOT_PATH, 0, -1).$uploads_path))
  126. {
  127. Helper::createDir(substr(ROOT_PATH, 0, -1).$uploads_path); //创建文件夹;
  128. }
  129. move_uploaded_file($file["tmp_name"], substr(ROOT_PATH, 0, -1).$image_path); */
  130. $image = AliyunOSS::uploadFile($image_path, $file['tmp_name']);
  131. if($image && $image['code']==1){}else{/* $this->error('系统错误'); */return ['code'=>0,'msg'=>'系统错误','data'=>''];}
  132. }
  133. }
  134. else
  135. {
  136. //$this->error('文件不得超过2M');
  137. return ['code'=>0,'msg'=>'文件不得超过2M','data'=>''];
  138. }
  139. $res[$key] = $image['data']['oss-request-url'];
  140. }
  141. }
  142. else
  143. {
  144. //$this->error('参数错误');
  145. return ['code'=>0,'msg'=>'参数错误','data'=>''];
  146. }
  147. return ['code'=>1,'msg'=>'操作成功','data'=>$res];
  148. }
  149. /**
  150. * 转换上传文件数组变量为正确的方式
  151. * @access public
  152. * @param array $files 上传的文件变量
  153. * @return array
  154. */
  155. public function dealFiles($files)
  156. {
  157. $fileArray = [];
  158. $n = 0;
  159. foreach ($files as $key => $file) {
  160. if (is_array($file['name'])) {
  161. $keys = array_keys($file);
  162. $count = count($file['name']);
  163. for ($i = 0; $i < $count; $i++) {
  164. $fileArray[$n]['key'] = $key;
  165. foreach ($keys as $_key) {
  166. $fileArray[$n][$_key] = $file[$_key][$i];
  167. }
  168. $n++;
  169. }
  170. } else {
  171. $fileArray = $files;
  172. break;
  173. }
  174. }
  175. return $fileArray;
  176. }
  177. /**
  178. * base64图片上传,成功返回路径,不含域名,只能单图上传
  179. * @param string img base64字符串
  180. * @return string
  181. */
  182. public function base64ImageUpload(Request $request)
  183. {
  184. $res = [];
  185. $base64_img = $_POST['img'];
  186. if($base64_img)
  187. {
  188. if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result))
  189. {
  190. $type = $result[2];
  191. if(in_array($type, array('jpeg','jpg','gif','bmp','png')))
  192. {
  193. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  194. $uploads_path = $this->path; //存储路径
  195. if(!file_exists($this->public_path.$uploads_path))
  196. {
  197. Helper::createDir($this->public_path.$uploads_path); //创建文件夹;
  198. }
  199. if(file_put_contents($this->public_path.$image_path, base64_decode(str_replace($result[1], '', $base64_img))))
  200. {
  201. return ReturnData::create(ReturnData::SUCCESS,null,$image_path);
  202. }
  203. else
  204. {
  205. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'图片上传失败');
  206. }
  207. }
  208. else
  209. {
  210. //文件类型错误
  211. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'图片上传类型错误');
  212. }
  213. }
  214. else
  215. {
  216. //文件错误
  217. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件错误');
  218. }
  219. }
  220. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'请上传文件');
  221. }
  222. }