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

(Symfony 4) How do I access the Liip Imagine bundle from within PHP code?

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

我的 liip/LiipImagineBundle 设置如下:

liip_imagine :

# 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' }

这是我正在查看的文档:https://symfony.com/doc/2.0/bundles/LiipImagineBundle/basic-usage.html#runtime-options

我遇到的问题是,在文档中它只是说要像下面那样做:

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

我得到的明显错误是:

Cannot use object of type App\Controller\CreatorDashboard\PostsController as array

我明白为什么会出现错误,它认为我正在尝试将我的控制器 class 用作数组。

但是你如何在代码中访问这个Liip/LiipImagineBundle呢?我如何在 Symfony 4 中获得 "handle" ?

文档不明确。

$this['imagine'] 的使用似乎仅在 PHP 模板中使用它时。要在控制器或服务中使用它,您可以 use it as a service,通过从容器中获取它(使用服务的旧方式),手动将其注入 class(在 service.yaml 文件 @liip_imagine.service.filter),或使用它提供的 class-as-a-service-name,就像您从 Request 对象、LoggerInterface 或Symfony 3.3+ 中的大多数其他内容(从代码中可以看出 Liip\ImagineBundle\Service\FilterService class)。

您分享的示例是在没有 twig 的情况下使用模板。如果您在控制器中(基于您共享的错误的假设),您需要从容器中取出 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 中测试) 例如,如果你想在服务(或控制器)中使用它,你可以注入 Liip\ImagineBundle\Imagine\Cache\CacheManager 并在您的 class:

中使用它
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;
}