123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Intervention\Image\Facades\Image;
- use Symfony\Component\Debug\Exception\FatalThrowableError;
- class UploadController extends Controller
- {
- public function uploadImage(Request $request)
- {
- if ($request->hasFile('image') && $request->file('image')->isValid()) {
- $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
- $kuoname = $request->file('image')->getClientOriginalExtension();
- $filePath = $request->file('image')->getRealPath();
- Log::debug('文件类型'.$kuoname);
- try{
- // if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) {
- // $width = Image::make($filePath)->width();
- // $height = Image::make($filePath)->height();
- // //要保存的文件名 时间+扩展名
- // $filename = time() . '_' . uniqid() . '.' . $kuoname;
- // $fileurl = $filename . '?' . $width . '_' . $height;
- // } else {
- //要保存的文件名 时间+扩展名
- $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
- // }
- $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
- Log::debug('upload_url:' . $imageUrl);
- if ($imageUrl) {
- return $this->jsonSuccess(['url' => config('customer.chxq_oss_url') . $path . $fileurl]);
- } else {
- return $this->jsonError('图片上传失败,请重试');
- }
- }catch (\Exception $exception){
- Log::error('upload-exception:'.$exception->getMessage());
- return $this->jsonError('图片上传失败,请重试');
- }catch (\Throwable $throwable){
- Log::error('upload-error:'.$throwable->getMessage());
- return $this->jsonError('图片上传失败,请重试');
- }
- } else {
- return $this->jsonError('仅支持图片上传,请重试');
- }
- }
- public function uploadImages(Request $request)
- {
- $files = $request->file('image');
- $fileCount = count($files);
- $urls = [];
- $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
- for ($i = 0; $i < $fileCount; $i++) {
- $kuoname = $files[$i]->getClientOriginalExtension();
- $filePath = $files[$i]->getRealPath();
- Log::debug('文件类型'.$kuoname);
- //要保存的文件名 时间+扩展名
- if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) {
- $width = Image::make($filePath)->width();
- $height = Image::make($filePath)->height();
- //要保存的文件名 时间+扩展名
- $filename = time() . '_' . uniqid() . '.' . $kuoname;
- $fileurl = $filename . '?' . $width . '_' . $height;
- } else {
- //要保存的文件名 时间+扩展名
- $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
- }
- $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
- if ($imageUrl) {
- array_push($urls, config('customer.chxq_oss_url') . $path . $fileurl);
- } else {
- Log::debug('文件上传失败。' . $filename);
- }
- }
- if ($urls) {
- return $this->jsonSuccess($urls);
- } else {
- return $this->jsonError('图片上传失败,请重试');
- }
- }
- }
|