显示来自网络目录之外的图像
Display images from outside of web directory
我需要显示存储在项目目录之外的图像。这是结构:
- symfony 项目
- 第二个项目/
- /src/
- /资产/
- /图片
提供直接路径无效。像这样:<img src="../../../second_project/src/assets/images/filename">
,使用 {{asset ('direct_path')
也没有完成工作。
我目前正在尝试使用控制器路径来执行此操作,它目前看起来像这样:
/**
* @Route("/admin/getphoto/{filename}", name="get_photo")
*/
public function getPhoto($filename) {
$path = realpath($this->getParameter('uploads_directory' . '/' . $filename));
return file($path, ['ContentType' => 'image/jpeg']);
}
我的模板如下所示:
<img src="{{ path('get_photo', {'filename' : photo.tag} ) }}" alt="">
/**
* @Route("/admin/getphoto/{filename}", name="get_photo")
*/
public function getPhoto($filename) {
$file = $this->getParameter('uploads_directory') . '/' . $filename;
$response = new Response();
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'image/png');
$response->setContent(file_get_contents($file));
return $response;
}
和
<img src="{{ path('get_photo', {'filename' : photo.tag} ) }}" alt="">
创造奇迹,谢谢!
我需要显示存储在项目目录之外的图像。这是结构:
- symfony 项目
- 第二个项目/
- /src/
- /资产/
- /图片
提供直接路径无效。像这样:<img src="../../../second_project/src/assets/images/filename">
,使用 {{asset ('direct_path')
也没有完成工作。
我目前正在尝试使用控制器路径来执行此操作,它目前看起来像这样:
/**
* @Route("/admin/getphoto/{filename}", name="get_photo")
*/
public function getPhoto($filename) {
$path = realpath($this->getParameter('uploads_directory' . '/' . $filename));
return file($path, ['ContentType' => 'image/jpeg']);
}
我的模板如下所示:
<img src="{{ path('get_photo', {'filename' : photo.tag} ) }}" alt="">
/**
* @Route("/admin/getphoto/{filename}", name="get_photo")
*/
public function getPhoto($filename) {
$file = $this->getParameter('uploads_directory') . '/' . $filename;
$response = new Response();
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'image/png');
$response->setContent(file_get_contents($file));
return $response;
}
和
<img src="{{ path('get_photo', {'filename' : photo.tag} ) }}" alt="">
创造奇迹,谢谢!