How to select BLOB files in PHP using Laravel

℡╲_俬逩灬. 提交于 2019-12-13 03:01:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!