问题
I have 50 images stored in SQL Database (BLOB). How can i select it and show it as an image in a view.
I have tried something like this
Route::get('/', function () {
$pics = DB::table('Employees')->get();
return $pics; //just to test if i get something return from the db
//return view('welcome', compact("pics"));
});
When i return the db query i get a very long string for an image.
FFD8FFE1001845786966000049492A00080000000000000000000000FFEC00114475636B7900010004000000500000FFE1031D687....
I have tried something like this with base64_encode
in my view:
@foreach ($pics as $pic)
<img src="base64_encode({{ $pic->image }})" />
@endforeach
unfortunately this is not working and i don't know how to encode it and return it as a path for the img
attribute src
.
回答1:
You need to convert the hex value to string, then encode the string by base64.
To convert the hex value to string:
$string = pack('H*', 'FFD8FFE1001845786966000049492A00080000000000000000000000FFEC00114475636B7900010004000000500000FFE1031D687');
Then encode the string by base64
$string = base64_encode($string);
Then you can use Data URI schema to display the image. Base on the file signatures, the hex string you given is jpg image. You should also change the mime type accordingly.
<img src="data:image/jpg;base64,<?=$string?>" />
来源:https://stackoverflow.com/questions/47755897/how-to-select-blob-files-in-php-using-laravel