Laravel-medialibrary: How To Get Base64 String?

Created on 14 Oct 2018  路  2Comments  路  Source: spatie/laravel-medialibrary

How do i get base64 JPG string as tiny placeholder instead of SVG for progressive image loading?

I need this because I am using this this package to work on a React Native app and React Native doesn't work well with SVG even as base64 string.

I am seeking for an implementation to help me get base64 JPG string instead of base64 SVG

Thanks For Your Help In Advance.

Most helpful comment

$path   = $media->getPath();
$type   = pathinfo($path, PATHINFO_EXTENSION);
$data   = file_get_contents($path);
$base64 = 'data:application/' . $type . ';base64,' . base64_encode($data);
return response()->json(['type' => $type, 'file' => $base64]);

All 2 comments

$path   = $media->getPath();
$type   = pathinfo($path, PATHINFO_EXTENSION);
$data   = file_get_contents($path);
$base64 = 'data:application/' . $type . ';base64,' . base64_encode($data);
return response()->json(['type' => $type, 'file' => $base64]);

This is worked for me. Thanks!!!!

Was this page helpful?
0 / 5 - 0 ratings