Symfony 5如何从没有公共目录粘贴图像到树枝



图片位于非公用文件夹。我想把图片返回给twig

我尝试使用binaryfilerresponse

$actualSignature = $this->getParameter('dir_signatures') .'/'.$actualSignature->getName();
$binaryFileSignature = new BinaryFileResponse($actualSignature);

并返回给twig

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}) }}"/>

最新更新