在我的项目中,我在web根目录之外有一些受保护的图像。
在我的控制器中,我创建了一个返回包含我想要显示的图像内容的BinaryFileResponse的路由:
/**
* @Route("/protected/images/{filename}", name="protected_image")
*/
public function getProtectedimage($filename)
{
//.. logic ..
return new BinaryFileResponse($path);
}在我的模板中,我使用Twig的path()来呈现该路由并显示图像:
<img src="{{ path('protected_image',{filename: myEntity.imagePath}) }}">现在,我希望与LiipImagine一起使用该路由应用filter并创建缩略图,如下所示:
<img src="{{ path('protected_image',{filename: myEntity.imagePath}) | imagine_filter('my_thumb_filter') }}">问题:总是在日志中显示破损的图像和Source image not found in ..。
我可以将LiipImagine与返回BinaryFileResponse的路由一起使用吗?如果是这样的话,是怎么做的?
发布于 2015-12-15 16:07:14
可以在返回响应之前应用筛选器。但是,这个解决方案并不有效,因为它对每个请求都应用了fitler,所以在包中使用CacheManager。(我还在琢磨它是否适用于非公众形象。)
/**
* @Route("/protected/images/{filename}", name="protected_image")
*/
public function getProtectedimage($filename)
{
//.. logic ..
/** @var \Liip\ImagineBundle\Binary\Loader\FileSystemLoader $loader */
$loader = $this->get('liip_imagine.binary.loader.filesystem');
/** @var \Liip\ImagineBundle\Imagine\Filter\FilterManager $filterManager */
$filterManager = $this->get('liip_imagine.filter.manager');
$filteredImageBinary = $filterManager->applyFilter(
$loader->find('path/to/image'),
'my_thumb_filter'
);
return new Response($filteredImageBinary->getContent(), 200, array(
'Content-Type' => $filteredImageBinary->getMimeType(),
));
}https://stackoverflow.com/questions/34293385
复制相似问题