1

I'm trying to upload base64 image to server. I've already try many answers, but i always get the same errors.

error

Links that i tried

How to save a PNG image server-side, from a base64 data string

https://www.reddit.com/r/laravel/comments/39wclp/how_to_convert_an_base64_image_string_to_an_image/

Convert Base64 string to an image file?

This code below should work.

$image = base64_decode(Input::get('image_cropped'));
$image_name= 'file_name.png';
$path = public_path() . "/images/" . $image_name;
file_put_contents($path, $image);
Community
  • 1
  • 1
Igor Ronner
  • 1,565
  • 2
  • 14
  • 31

1 Answers1

0

If you inspect the form sent from request the image encoded starts with data:image/png;base64, so try with:

$img = explode(',', Input::get('image_cropped'));
$img_name = 'file_name.png';
file_put_contents(public_path().'/images/'.$img_name, base64_decode($img[1]));

This worked for me.

Jose Rojas
  • 3,490
  • 3
  • 26
  • 40