Laravel从base64返回图像预览

时间:2018-01-29 07:37:11

标签: image laravel base64

我有base64图像,我想返回图像预览而不是base64代码。

我试过

return response(base64_decode($results->getBase64Image()), 200, ['Content-Type' => 'image/png']);

但它返回一个奇怪的输出。 Result

5 个答案:

答案 0 :(得分:0)

我一直用这个..用于基础64图像))))

function store()
{
    $data = $this->request->img;
    list($type, $data) = explode(';', $data);
    list(, $data) = explode(',', $data);

    $directory_bucekts = implode('/',str_split(strtolower(str_random(2))));

    Storage::disk('public')->makeDirectory('images/shopdata/'.$directory_bucekts, true, true);

//        mkdir(storage_path('app/public/images/shopdata/'.$directory_bucekts), 755, true);

    $data         = base64_decode($data);

    $fileName     = time().rand(100,1000).'.jpg';
    $path         = storage_path('app/public/images/shopdata/'.$directory_bucekts);

    file_put_contents($path .'/'. $fileName, $data);

    return response()->make(['path' =>  '/storage/images/shopdata/' . $directory_bucekts. '/' .$fileName]);

}

答案 1 :(得分:0)

试试这个:

        $data = base64_decode($results->getBase64Image());
        $image_name= time().'_test_.png';
        $path = public_path() .'/'. $image_name;

        file_put_contents($path, $data);

        //serve the image
       return  response()->file($path);

请参阅Docs

答案 2 :(得分:0)

您可以将base64代码返回到视图,并在视图中使用<img>代码

渲染图片
$base64code = $results->getBase64Image();
return view('preview', compact('base64code'));

刀片文件:

<img src="data:image/png;base64, {{ $base64code }}" alt="Image Preview" />

答案 3 :(得分:0)

我刚刚找到答案。 imagecreatefromjpeg() php

$image = imagecreatefromstring(base64_decode($results->getBase64Image()));
        header('Content-type: image/png');
        return imagejpeg($image);

答案 4 :(得分:0)

Laravel 5.8及更高版本...

直接将字符串放入response() ... 并添加header('Content-Type', 'image/png')

例如:

$raw_image_string = base64_decode($base64_img_string);

return response($raw_image_string)->header('Content-Type', 'image/png');