Symfony 3-在树枝延伸范围内提供图像

问题描述 投票:0回答:1

我想使用php例如在path/to/myphp/script.php?image=imagereference&some=parameter的公共文件夹(web)中提供一些不可用的图像>

为了提高性能而不使用this approach,我做了一个小树枝扩展名来这样做。

<?php

#src/MyBundle/Service/DisplayImage.php

namespace MyBundle\Service;

#https://symfony.com/blog/new-in-symfony-2-4-the-request-stack
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;

class DisplayImage
{
    private $entityManagerInterface;
    private $requestStack;

    public function __construct(EntityManagerInterface $entityManagerInterface, RequestStack $requestStack)
    {
        $this->entityManagerInterface = $entityManagerInterface;
        $this->requestStack = $requestStack;
    }

    public function show(int $ref)
    {
        $photoPath = __DIR__ ."/../photoFolder/".$ref.".jpg";
        $file = file_get_contents($photoPath);

        $response = new Response();
        $response->headers->set('Content-Type', 'image/jpeg');
        $response->setContent($file);
        return $response;
    }
}

进入我的树枝模板

{# src/MyBundle/Ressources/views/DisplayImage.html.twig #}

{% for ref in refs %}
  <img src="{{ show(ref) }}"/>
{% endfor %}

但是它不起作用,因为返回的响应不是有效的src路径。

因此,我的问题是如何生成针对我的树枝扩展名的URL?

path/to/twigExtension.php?ref=ref之类的呼叫show(ref)

也许不是实现这一目标的好方法

<< [

这是一个两步过程。

    您需要创建一个指向每个图像的有效链接(这将是通往某种
  1. ImageController :: show(id)控制器操作的路径)
] >>

ImageController接受ID(或图像名称)并大声播放图像文件的二进制内容(如果未授予用户下载图像的权限,则抛出错误)。您可以在此处返回BinaryFileResponse。看看here

php symfony twig
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.