(Symfony 4) 如何从 PHP 代码中访问 Liip Imagine 包? [英] (Symfony 4) How do I access the Liip Imagine bundle from within PHP code?
问题描述
我希望能够上传文件,并从中创建 3 个缩略图并将所有内容存储在 S3 服务器上.
I want to be able to upload a file, and create 3 thumbnails from it and store everything on an S3 server.
我的 liip/LiipImagineBundle 设置如下:
I have my liip/LiipImagineBundle set up as follows:
liip_imagine :
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 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')
我得到的明显错误是:
不能使用 App\Controller\CreatorDashboard\PostsController 类型的对象作为数组
我明白为什么我会收到错误,它认为我正在尝试将我的控制器类用作数组.
I understand why I am getting the error, it thinks I'm trying to use my controller class as an array.
但是你如何在代码中访问这个 Liip/LiipImagineBundle 呢?如何在 Symfony 4 中获得句柄"?
But how do you access this Liip/LiipImagineBundle in the code then? How do I get a "handle" on it in Symfony 4?
文档不清楚.
推荐答案
您分享的示例是针对没有 twig 的模板使用.如果您在控制器中(基于您共享的错误的假设),您需要从容器中移除 Liip 缓存管理器.
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
这篇关于(Symfony 4) 如何从 PHP 代码中访问 Liip Imagine 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!