简体   繁体   English

HTTP,PHP:下载返回base64图像png

[英]HTTP, PHP: Return base64 image png as download

I have a base64 representation of an png image and want to return a download stream of that image. 我有一个png图像的base64表示形式,想要返回该图像的下载流。

So when the user opens the url, a download of that image is returned. 因此,当用户打开URL时,将返回该图像的下载。 How can I achieve this in PHP? 如何在PHP中实现呢? I'm using Symfony 2.5 我正在使用Symfony 2.5

I suppose you are in a Controller class of the Symfony framework, so is better you deal with the corrispondent framework object as follow: 我想您位于Symfony框架的Controller类中,因此最好按以下方式处理对应的框架对象:

public function imageDownloadAction()
{
  .....
    // Symfony\Component\HttpFoundation\Response 
    $response = new Response();
    $response->setContent($image);
    $response->headers->set('Content-Type', 'application/octet-stream');
    $response->headers->set('Content-Disposition', 'attachment; filename='.$filename);

    return $response;

}

You can try something like: 您可以尝试如下操作:

header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: application/octet-stream'); 
echo base64_decode($base64strImg);

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

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