![](/img/trans.png)
[英]How to load images in twig from root directory folder in Symfony 2?
[英]Symfony 5 how paste image to twig from no public directory
图像位于非公共文件夹中。 我想将图像返回到树枝。
我尝试使用 BinaryFileResponse
$actualSignature = $this->getParameter('dir_signatures') .'/'.$actualSignature->getName();
$binaryFileSignature = new BinaryFileResponse($actualSignature);
并将其归还给树枝
return $this->render('security/profile.html.twig',['form' => $form->createView(),'signature' => $binaryFileSignature]);
在树枝上
<img src="{{ signature.file }}"/>
您可以制作一个仅返回图像的控制器动作,如下所示:
/**
* @Route("image/{imagePath}", name="show_image")
*/
public function showimageAction(string $imagePath): BinaryFileResponse
{
$response = new BinaryFileResponse(
$this->getParameter('kernel.project_dir')
. '/'
. $this->getParameter('dir_signatures') // related to project root dir
. '/'
. $imagePath
);
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT
);
return $response;
}
然后将图像名称传递给您的模板以获取:
return $this->render('security/profile.html.twig',[
'form' => $form->createView(),
'signature' => $actualSignature->getName()
]);
在模板中:
<img src="{{ path('show_image', {imagePath: signature}) }}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.