Last active
August 15, 2019 17:56
-
-
Save mccabiles/df7ddb34c09a31664d6ac423839a4299 to your computer and use it in GitHub Desktop.
Base64 Image uploading for Laravel 5.6+ using Storage Facade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Illuminate\Support\Facades\Storage; | |
//... | |
$base64image = '...'; | |
@list($type, $file_data) = explode(';', $base64image); | |
@list(, $file_data) = explode(',', $file_data); | |
$type = explode(";", explode("/", $base64image)[1])[0]; | |
$path = 'images/' . time() . '.' . $type; | |
Storage::disk('local')->put($path, base64_decode($file_data)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('images/{file}', function($file) | |
{ | |
$type = pathinfo($file); // get file type | |
$file = Storage::disk(env('STORAGE_DRIVER', 'local'))->get('images/'.$file); // retrieve the file from storage | |
$response = Response::make($file, 200); | |
$response->header("Content-Type", $type); | |
return $response; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment