I am at the end of my wisdom trying to use the StoreAs method in Laravel to store an image that has not been attached by the html file handler and has been sent through request being of type "File".
I have created a cropped image using croppie: https://foliotek.github.io/Croppie/
However, croppie does not seem to provide me with an easy solution to attach the cropped image to the standard html filehandler. So I decided to attach the image information and the image name to a hidden file element and send it along with my post request to my laravel instance. However, my problem is now that I want to use the StoreAs method, because it allows me to attach my image to a specific url which I can retrieve which I find very useful, when retrieving a big amount of images.
The StoreAs() method in Laravel apparently only allows to use an object of type UploadedFile, which is based on the Symfony\Component\HttpFoundation\File\UploadedFile class. This UploadedFile object apparently is sent automatically along with the file when using the filehandler in html.
I have a base64 encoded image similar to this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4nOy9d3RUZ5ruuyvnnJNyzpEgcg4mO+e23U7t3Hl6wu2ZOTPn3Dtn8unTTTAYMGBjbDAYCZRBApRIkkpSqZ and an image name.
I found that I can create a UploadedFile object in Laravel myself but I struggle to understand how I have to create this UploadedFile and then use the StoreAs() method to link it to a generated url.
I found this link here: Laravel - file path to UploadedFile instance And this one here: https://gist.github.com/m241802/0b5d0a98318304ff08cf
But I still do not completely get how I can create an UploadedFile object out of the image I have and the image name.
Does anyone have an idea how to accomplish this? Or even can suggest a more elegant solution to my problem?? Perhaps someone has a better solution as to how I can send an File object in the post request after having used the croppie library?
Thanks in advance for any help!
Try this :
$image = $request->input('image'); // your base64 encoded
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = 'test.png';
\File::put(storage_path(). '/' . $imageName, base64_decode($image));