我有一些使用Symfony创建的图像,它们生成为:
src="data:image;base64,/9j/4AAQSkZJRg... etc"
data-holder-rendered="true"
在我的控制器中使用此代码:
$sql = "select id,nombre,descripcion,encode(imagen, 'base64') as imagen from data.servicios_mapa order by destacado DESC, fecha_creacion DESC LIMIT 4" ;
$stmt = $conn->prepare( $sql );
$stmt->execute();
$mapas = $stmt->fetchAll();
$datos_mapa = array();
foreach($mapas as $m){
if($m['imagen'] != '' || $m['imagen'] != null){
$m['imagen'] = "data:image;base64,".$m['imagen'];
}else{
$m['imagen'] = "/imgs/no-image.jpg";
}
$datos_mapa[] = $m;
}
return $this->render('FrontendBundle:Default:index.html.twig', array("estructura"=>json_encode(array()),"datosmapas"=>$datos_mapa) );
然后在Twig:
<img src="{{ mapa.imagen }}" class="img-responsive fix-height">
但其中一些是错误的,如左图所示:
http://fotos.subefotos.com/5909f04a9cfa9be2c4b286d1b87727eco.png
什么可能是reasson?它们都是用相同的代码生成的。
答案 0 :(得分:1)
在您的循环中,当if($m['imagen'] != '' || $m['imagen'] != null)
或$m['imagen'] == null
$m['imagen'] == ''
的其他内容
因为null!=''给出true,''!= null给出true
所以你将拥有<img src="data:image;base64," class="img-responsive fix-height">
您应该将其更改为:if($m['imagen'] != '' && $m['imagen'] != null)