mardi 13 novembre 2018

Laravel - Convert a base64 string to a valid image

To give you a grasp of what I am trying to do, here is my current code

This code is run when a post request is made to specific URL

public function uploadImage(Request $request) {
    $request->file = base64_decode(explode(',', $request->file)[1]);

    $request->validate([
        'file' => 'image|required|mimes:jpg,png',
    ]);

    $image = $request->file('file');

    $complete = $image->getClientOriginalName();
    $name = pathinfo($complete, PATHINFO_FILENAME);
    $extension = $image->getClientOriginalExtension();
    $storageName = $name.'_'.time().'.'.$extension;

    Storage::disk('public')->put($storageName, File::get($image));

    return Storage::disk('public')->path($storageName);
}

On the first line I am trying to be smart and first decode the base64 to a file (if I am correct?).

Next is a validation to validate if the file parameter in the request exists, is an image, and is a .jpg or .png

(The next lines is just about saving the "image" to the filesystem)

But the validation won't pass because the file parameter is not an image. So my question is: is it possible to convert a base64 string to a valid image in Laravel? If it is, how can this be achieved?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire