Пример #1
0
 /**
  * Upload file to Amazon s3
  *
  * @param  UploadedFile $file
  * @param  String $subdirectory
  * @param  String $type
  * @return Array $uploadedFile
  */
 private function s3Upload($file, $subdirectory, $type)
 {
     $client_original_name = $file->getClientOriginalName();
     $fileName = time() . '_' . $client_original_name;
     $destinationPath = 'uploads/' . $subdirectory;
     $path = $destinationPath . '/' . $fileName;
     $image = Image::make($file->getRealPath());
     switch ($type) {
         case 'profile_photo':
             $image->fit(128, 128, function ($constraint) {
                 $constraint->upsize();
             });
             break;
         case 'profile_cover':
             $image->resize(1440, null, function ($constraint) {
                 $constraint->aspectRatio();
                 $constraint->upsize();
             });
             break;
     }
     $stream = $image->stream();
     $s3 = Storage::disk('s3');
     $s3->put($path, $stream->__toString(), 'public');
     $client = $s3->getDriver()->getAdapter()->getClient();
     $public_url = $client->getObjectUrl(env('S3_BUCKET'), $path);
     $original_name = pathinfo($client_original_name, PATHINFO_FILENAME);
     $uploadedFile = ['original_name' => $original_name, 'file_name' => $fileName, 'public_url' => $public_url, 'type' => $type];
     return $uploadedFile;
 }