LiipImagineBundle 和 BinaryFileResponse

LiipImagineBundle and BinaryFileResponse

在我的项目中,我在网络根目录之外有一些受保护的图像。

在我的控制器中,我创建了一个 returns 一个 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 与 returns BinaryFileResponse 的路线一起使用吗?如果可以,怎么做?

您可以在返回响应之前应用过滤器。但是,此解决方案效率不高,因为它对每个请求都应用了 fitler,因此请使用捆绑包中的 CacheManager。 (我仍在弄清楚它是否适用于非 public 图像。)

/**
 * @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(),
    ));
}