0

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?

FlyingUnderpants
  • 101
  • 1
  • 3
  • 12

3 Answers3

1

You can valid image base64
please follow me step by step :

1 . Go to folder yourProjectName\app\Providers\AppServiceProvider.php
2 . copy this code and past in this file at function boot()

Validator::extend('is_image', function ($attribute, $value, $parameters, $validator) {
        preg_match_all('/([^\.]+)\.([a-zA-Z]+)/',$value,$matchedExt);
        if (isset($matchedExt[2][0]) && in_array($matchedExt[2][0],$parameters)) return true;
        preg_match_all('/data\:image\/([a-zA-Z]+)\;base64/',$value,$matched);
        $ext = isset($matched[1][0]) ? $matched[1][0] : false;
        print_r($value);
        return in_array($ext,$parameters) ? true : false;
});
  1. Go to your function uploadImage() and past this code
public function uploadImage(Request $request) {

    $request->validate([
        'file' => 'image|required|is_image: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);
}

I hope I'll solve the problem for you

0

Laravel can't resolve your base 64 string to a file, hence the failure of the file validator. You also won't be able to access your data with ->file('file'), since again, you're not sending over a valid file upload.

Instead, you could do your own simple string check (perhaps index of data:image or preferably something more extensive, like imagecreatefromstring) or look into writing a custom Laravel validation rule, and then save the sent string into a file with file_put_contents.

If you're actually wanting to do a file upload and convert that to base 64 on the server side, then you'll want to use an <input type="file">. From there, you'll be able to use built-in Laravel validation and file storage functions. Refer to this extensive guide. Once you've gotten your file, you can convert it to base64 using base64_encode (see this answer for a more thorough guide).

I realize this answer wasn't very specific, but if you clarify exactly what you're trying to do, I can give more hands-on guidance.

sheng
  • 1,226
  • 8
  • 17
-1

I'm using this code to convert a base64 string to an image :

    public function getImage($img, $pid)
{
    if($img == "")
        return null;
    $imagecode = base64_decode($img);

    $directory = public_path('img/uploads/x' . $pid);
    if (!file_exists($directory))
        \File::makeDirectory($directory);
    $id = uniqid('img_');
    $filename = time() . '-' . $id . '.png';
    $path = public_path('img/uploads/product_img/p' . $pid . '/' . $filename);
    $tpath = public_path('img/uploads/product_img/p' . $pid . '/small-' . $filename);

    try {
        $image = Image::make($imagecode)->widen(600, function ($constraint) {
            $constraint->upsize();
        })->save($path);
        $image->fit(200, 200)->save($tpath);
    } catch (Exception $e) {
        return null;
    }

    return "x" . $pid . "/" . $filename;
}
Rezasys
  • 153
  • 2
  • 12