2016-05-12 9 views
0

SQL ServerデータベースにBLOB列の画像があります。Silex + Twig + SQL Server:画像のレンダリング

私はそれらを取得し、HTMLページに表示する必要があります。

しかし、HTMLページの結果では、写真の代わりにsrcという属性の長い文字列があります。

サイレックスコード:

<!-- 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; 
} 

小枝ファイル:

<!-- 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 %} 

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> 

それの何が問題になっているのですか?

答えて

2

あなたは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 }} 
関連する問題