简体   繁体   English

Symfony2-树枝中的base64_decode

[英]Symfony2 - base64_decode in twig

I need to display a picture in my twig template. 我需要在树枝模板中显示图片。 I get this picture in my controller as string encoded in base64, so I created a personal function in my service to decode the base64 : 我在控制器中得到的图片是用base64编码的字符串,因此我在服务中创建了一个个人函数来解码base64:

public function base64ToImg($base64)
{
    $img_str = 'image/png;base64,'.$base64;
    $img_data = explode(";",$img_str);
    $type_img = $img_data[0];
    $final_img = explode(",",$img_data[1]);
    header("Content-type:".$type_img);

    return base64_decode($final_img[1]);
}

and in my controller : 在我的控制器中:

$logo = $this->container->get('services.utils')->base64ToImg($mydata);
echo $logo;
die();

It works, but when i send $logo to my template with a render , the picture isn't displayed with {{ logo }} . 它可以工作,但是当我使用render$logo发送到模板时,图片没有显示{{ logo }} I tried to create my own twig function too but doesn't works also... 我也尝试创建自己的树枝函数,但也无法正常工作...

There is a solution ? 有解决办法吗?

Thanks 谢谢

How are you trying to display the image inside the template? 您如何尝试在模板中显示图像? If you use the base64encoded string you'd probably need data:image/png;base64,encoded string in your image tag. 如果您使用data:image/png;base64,encoded string ,则可能需要在image标签中使用data:image/png;base64,encoded string

<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

Other options are saving to file or using a script to display the image 其他选项是保存到文件或使用脚本显示图像

<img alt="Embedded Image" src="/controller/image?id=foo" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM