gpt4 book ai didi

image - 在 symfony 2 中显示 blob 图像

转载 作者:行者123 更新时间:2023-12-02 01:48:45 25 4
gpt4 key购买 nike

我需要在我的 symfony 2 应用程序中显示存储在我的数据库中的图像。
我进行了搜索并找到了示例,例如:

Php Display Image Blob from Mysql

行:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

它对我不起作用。如何返回图像文件并将其显示在用户的个人资料或图库页面中?

最佳答案

我创建了这个在我的表中搜索 blob 数据的操作:Empleado

    /**
* @Route("/employee/photo/{id}", name="zk_time_employee_photo", requirements={"id" = "\d+"}, defaults={"id" = 1})
*/
public function photoAction()
{
$request = $this->getRequest();
$workerId = $request->get('id');
if (empty($workerId))
//throw exception

$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('ZkTimeBundle:Empleado');

$photo = $repository->findPhoto($workerId);
if (empty($photo))
//throw exception

$response = new StreamedResponse(function () use ($photo) {
echo stream_get_contents($photo);
});

$response->headers->set('Content-Type', 'image/png');
return $response;
}

这部分是必不可少的(适用于两种格式,无论存储的原始格式如何):

 $response->headers->set('Content-Type', 'image/png');<br>
OR
$response->headers->set('Content-Type', 'image/jpeg');<br>

为了显示它我选择了:

<img src="{{ asset(path('zk_time_employee_photo', {'id': employee.Id})) }}"/>

多张图片(轮播)或画廊的基础是相同的。
我希望这会有所帮助,因为网络上关于它的结果是空的,或者没有集中在 Symfony2 中。主要是纯PHP。

这些人非常有帮助:

Answer #1: symfony2-how-to-display-download-a-blob-field
Answer #2: symfony2-path-to-image-in-twig-template

关于image - 在 symfony 2 中显示 blob 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com