如何使用Laravel在PHP中选择BLOB文件 [英] How to select BLOB files in PHP using Laravel

查看:62
本文介绍了如何使用Laravel在PHP中选择BLOB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL数据库(BLOB)中存储了50张图像.如何选择它并在视图中将其显示为图像.

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....

在我看来,我已经尝试过使用base64_encode这样的事情:

I have tried something like this with base64_encode in my view:

  @foreach ($pics as $pic)

        <img src="base64_encode({{ $pic->image }})" />

  @endforeach

不幸的是,这不起作用,我也不知道如何编码并将其作为img属性src的路径返回.

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.

推荐答案

您需要将十六进制值转换为字符串,然后通过base64对该字符串进行编码.

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');

然后通过base64对字符串进行编码

Then encode the string by base64

$string = base64_encode($string);

然后,您可以使用数据URI模式来显示图像.根据文件签名,您提供的十六进制字符串为jpg图片.您还应该相应地更改MIME类型.

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?>" />

这篇关于如何使用Laravel在PHP中选择BLOB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆