(Symfony的4)如何从PHP代码中访问LIIP想象一下包吗?

问题描述 投票:2回答:2

我希望能够上传文件,并从它创建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

我明白为什么我得到的错误,它认为我试图用我的控制器类作为一个数组。

但你如何在代码中访问此LIIP / LiipImagineBundle呢?如何获得在Symfony的4上有一个“把柄”?

该文档是不明确的。

symfony thumbnails
2个回答
3
投票

您共享的例子是没有枝杈模板使用。如果你在一个控制器(根据您提供的错误的假设),你需要得到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

1
投票

使用$this['imagine']的出现在PHP模板使用时,它只能是。对于一个控制器,或服务中使用它,你会use it as a service,无论是得到它从容器(使用服务的旧样式),手动将其注入类(与@liip_imagine.service.filter的service.yaml文件),或者使用类作为一种服务的名称,它提供,以同样的方式,你会的类型提示从Request objec,LoggerInterface,或Symfony的3.3+(大多数其他任何东西从代码,出现在Liip\ImagineBundle\Service\FilterService类)。

© www.soinside.com 2019 - 2024. All rights reserved.