Skip to content

Instantly share code, notes, and snippets.

@lpirir
Created September 29, 2020 03:00
Show Gist options
  • Save lpirir/ffcb777034147316521694acab793eb1 to your computer and use it in GitHub Desktop.
Save lpirir/ffcb777034147316521694acab793eb1 to your computer and use it in GitHub Desktop.
This code store a base64 image using the Laravel's filesytem (File Storage) methods
$image_64 = $data['photo']; //your base64 encoded data
$extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1]; // .jpg .png .pdf
$replace = substr($image_64, 0, strpos($image_64, ',')+1);
// find substring fro replace here eg: data:image/png;base64,
$image = str_replace($replace, '', $image_64);
$image = str_replace(' ', '+', $image);
$imageName = Str::random(10).'.'.$extension;
Storage::disk('public')->put($imageName, base64_decode($image));
@Nemachtilli
Copy link

Hola trato de usar esté código y me da el siguiente error "Undefined array key 1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment