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

孤街醉人 提交于 2020-01-24 18:30:13

问题


I want to be able to upload a file, and create 3 thumbnails from it and store everything on an S3 server.

I have my liip/LiipImagineBundle set up as follows:

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

This is the documentation I am looking at: https://symfony.com/doc/2.0/bundles/LiipImagineBundle/basic-usage.html#runtime-options

The problem I am having is that in the documentation it just says to do it like the following:

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

The obvious error I am getting is:

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

I understand why I am getting the error, it thinks I'm trying to use my controller class as an array.

But how do you access this Liip/LiipImagineBundle in the code then? How do I get a "handle" on it in Symfony 4?

The documentation isn't clear.


回答1:


The example you shared is for template usage without twig. If you're in a controller (an assumption based on the error you shared) you need to get the Liip Cache Manager off of container.

/** @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



回答2:


The use of $this['imagine'] appears to only be when using it in a PHP-template. For using it within a controller, or service, you would use it as a service, either by getting it from the container (the old style of using a service), injecting it manually into a class (in the service.yaml file with @liip_imagine.service.filter), or using the class-as-a-service-name that it provides, in the same way you would type-hint anything from the Request objec, LoggerInterface, or most other things in Symfony 3.3+ (from the code, that appears to the Liip\ImagineBundle\Service\FilterService class).



来源:https://stackoverflow.com/questions/54543563/symfony-4-how-do-i-access-the-liip-imagine-bundle-from-within-php-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!