(Symfony 4)如何从PHP代码中访问Liip Imagine捆绑包?

(Symfony 4)如何从PHP代码中访问Liip Imagine捆绑包?,symfony,thumbnails,Symfony,Thumbnails,我希望能够上传一个文件,并从中创建3个缩略图,并将所有内容存储在S3服务器上 我的liip/LiipImagineBundle设置如下: 想象一下: # configure resolvers resolvers : # setup the default resolver default : # use the default web path web_path : ~ # your filter sets are defined here

我希望能够上传一个文件,并从中创建3个缩略图,并将所有内容存储在S3服务器上

我的liip/LiipImagineBundle设置如下:

想象一下:

# configure resolvers
resolvers :

    # setup the default resolver
    default :

        # use the default web path
        web_path : ~

# your filter sets are defined here
filter_sets :

    # use the default cache configuration
    cache : ~

    # the name of the "filter set"
    my_thumb :

        # adjust the image quality to 75%
        quality : 75

        # list of transformations to apply (the "filters")
        filters :

            # create a thumbnail: set size to 120x90 and use the "outbound" mode
            # to crop the image when the size ratio of the input differs
            thumbnail  : { size : [120, 90], mode : outbound }

            # create a 2px black border: center the thumbnail on a black background
            # 4px larger to create a 2px border around the final image
            background : { size : [124, 94], position : center, color : '#000000' }

    # the name of the "filter set"
    thumb_square :

        # adjust the image quality to 75%
        quality : 75

        # list of transformations to apply (the "filters")
        filters :

            thumbnail :  { size : [300, 300], mode : outbound }

            # create a 2px black border: center the thumbnail on a black background
            # 4px larger to create a 2px border around the final image
            background : { size : [304, 304], position : center, color : '#000000' }

    # the name of the "filter set"
    thumb_rectangle_md :

        # adjust the image quality to 75%
        quality : 75

        # list of transformations to apply (the "filters")
        filters :

            thumbnail :  { size : [670, 400], mode : outbound }

            # create a 2px black border: center the thumbnail on a black background
            # 4px larger to create a 2px border around the final image
            background : { size : [674, 404], position : center, color : '#000000' }

    # the name of the "filter set"
    thumb_hd :

        # adjust the image quality to 75%
        quality : 75

        # list of transformations to apply (the "filters")
        filters :

            thumbnail :  { size : [1920, 1080], mode : outbound }

            # create a 2px black border: center the thumbnail on a black background
            # 4px larger to create a 2px border around the final image
            background : { size : [1924, 1084], position : center, color : '#000000' }
这是我正在查看的文档:

我遇到的问题是,在文档中,它只是说按如下方式执行:

$this['imagine']->过滤器('/relative/path/to/image.jpg','my_thumb')

我得到的明显错误是:

无法将App\Controller\CreatorDashboard\PostsController类型的对象用作数组

我理解为什么会出现错误,它认为我试图将控制器类用作数组

但是如何在代码中访问这个Liip/LiipImagineBundle呢?如何在Symfony 4中获得“句柄”


文件不清楚

似乎只有在PHP模板中使用
$this['imagine']
时才使用它。要在控制器或服务中使用它,您可以从容器中获取它(使用服务的旧样式),手动将其注入类中(在带有
@liip\u imagine.service.filter
的service.yaml文件中),或者使用它提供的class-as-a-service-name,以同样的方式,您可以键入来自请求对象、LoggerInterface或Symfony 3.3+中大多数其他内容的任何提示(来自出现在
Liip\ImagineBundle\Service\FilterService
类中的代码)。

您共享的示例用于不带细枝的模板使用。如果您在控制器中(基于您共享的错误的假设),则需要将Liip缓存管理器从容器中取出

/** @var CacheManager */
$imagineCacheManager = $this->get('liip_imagine.cache.manager'); // gets the service from the container

/** @var string */
$resolvedPath = $imagineCacheManager->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb'); // resolves the stored path
(在Symfony 5中测试) 如果您想在intance服务(或控制器)中使用它,可以插入Liip\ImagineBundle\Imagine\Cache\CacheManager 并在课堂上使用它:

public function __construct(UploaderHelper $uploaderHelper, CacheManager $cacheManager)
{
    $this->uploaderHelper = $uploaderHelper;
    $this->cacheManager = $cacheManager;
}   
public function getImageLink($image)
{
    $imagePath = $this->uploaderHelper->asset($image, 'imageFile');
    $imageLink = $this->cacheManager->getBrowserPath($imagePath, $imagine_filter);
    return $imageLink;
} 

谢谢harmstyler,成功了。我在想,你是如何让它在服务课上发挥作用的?因为我希望遵循最佳实践并尽可能使控制器轻。必须将其作为id为
liip\u imagine.cache.manager的参数传递