在symfony 2中显示blob图像 [英] Display blob image in symfony 2

查看:168
本文介绍了在symfony 2中显示blob图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在symfony 2应用程序中显示存储在我数据库中的图像。

我搜索了一下,我发现了一些例子:

I need to display images stored in my database, from users in my symfony 2 application.
I've searched and I found examples such as:

来自Mysql的Php显示图像Blob

行:

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

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

It didn't worked for me. How to return an image file and display it in user's profile or a gallery page?

推荐答案

我创建了此动作搜索表格中的blob数据: Empleado

I created this action that searches the blob data in my table: 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;
    }

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

This part is essential (works for both formats, no matters the original format stored):

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

为了显示它,我选择了:

In order to display it I chose:

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

多个图像( carousel )的基数相同或< b> gallery 。

我希望这会有所帮助,因为网络上有关于它的空结果或者没有关注Symfony2。主要是纯PHP。

The base is the same for multiple images (carousel) or a gallery.
I hope this helps, because the web has empty results about it or not focused in Symfony2. Mainly, pure PHP.

答案#1:symfony2-how-to-display-download-a-blob-field

回答#2:symfony2-path-to-image-in-twig-template

这篇关于在symfony 2中显示blob图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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