Database Silex&x2B;小枝+;SQL Server:渲染图片

Database Silex&x2B;小枝+;SQL Server:渲染图片,database,image,twig,silex,Database,Image,Twig,Silex,我在SQL Server数据库中的BLOB列中有一些图片 我需要得到它们并显示到HTML页面 但是在HTML页面的结果中,我在src属性中使用了长字符串,而不是图片 Silex代码: <!-- language-all: php --> $pict->get('/show_pic/{id}', function ($id) use ($app) { //Request of photos $photos=getPhotoByID($id, $ap

我在SQL Server数据库中的BLOB列中有一些图片

我需要得到它们并显示到HTML页面

但是在HTML页面的结果中,我在
src
属性中使用了长字符串,而不是图片

Silex代码:

<!-- language-all: php -->
$pict->get('/show_pic/{id}', function ($id) use ($app) {
        //Request of photos
        $photos=getPhotoByID($id, $app);
        return $app['twig']->render('test/template_show_pictures.html.twig', array('photos'=>$photos));
})->bind('show_pic');

function getPhotoByID($ID, $app)
{
        $sql = "<some SQL here>";
        $statement = $app['dbs']['mssql']->prepare($sql);
        $statement->bindValue(1, $ID, "text");
        $statement->execute();
        $photo = $statement->fetchAll();

        if (empty($photo)) {
            return "We don't have a photo with id=".$ID;
        }

        return $photo;
}

$pict->get('/show_pic/{id}',函数($id)use($app){
//索取照片
$photos=getPhotoByID($id$app);
返回$app['twig']->render('test/template_show_pictures.html.twig',数组('photos'=>$photos));
})->绑定('show_pic');
函数getPhotoByID($ID,$app)
{
$sql=“”;
$statement=$app['dbs']['mssql']->prepare($sql);
$statement->bindValue(1,$ID,“text”);
$statement->execute();
$photo=$statement->fetchAll();
如果(空($photo)){
return“我们没有id为“”的照片,$id;
}
返回$photo;
}
细枝文件:

<!-- language: twig -->
    {% extends 'test/test_base.html.twig' %}
    {% block title %}Pictures from DB{% endblock %}
    {% block content %}
    {% for im in photos %}
      <img alt="Embedded Image" src="data:image/jpeg;base64,{{ im['Foto'] }}" />
    {% endfor %}
    {% endblock %}

{%extends'test/test_base.html.twig%}
{%block title%}来自DB的图片{%endblock%}
{%block content%}
{%用于照片中的即时消息%}
{%endfor%}
{%endblock%}
HTML标记:

<!-- language: lang-html -->
<!DOCTYPE html>
<html>
    <head>
        <title>Pictures from DB</title>
            <link rel="stylesheet" href="/css/print.css">
        </head>
    <body>
    <div class="page">
      <img alt="Embedded Image" src="data:image/jpeg;base64,���JFIF...too many symbols here" />
            </div>
    </body>
</html>

来自数据库的图片

有什么问题吗?

您应该使用base64对图像进行编码

将用于编码的筛选器添加到base64:

$app->extend('twig', function ($twig, $app) {
    $twig->addFilter('base64_encode', new \Twig_SimpleFilter('base64_encode', function ($value) {
        return \base64_encode($value);
    }));

    return $twig;
});
并在模板中使用它:

{{ im['Foto']|base64_encode }}

您应该使用base64对图像进行编码

将用于编码的筛选器添加到base64:

$app->extend('twig', function ($twig, $app) {
    $twig->addFilter('base64_encode', new \Twig_SimpleFilter('base64_encode', function ($value) {
        return \base64_encode($value);
    }));

    return $twig;
});
并在模板中使用它:

{{ im['Foto']|base64_encode }}