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

dauxcl2d  于 2023-10-24  发布在  PHP
关注(0)|答案(4)|浏览(128)

我希望能够上传一个文件,并从它创建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中获得它的“句柄”呢?
文件不清楚。

mgdq6dx1

mgdq6dx11#

你分享的例子是不带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
0wi1tuuw

0wi1tuuw2#

(在Symfony 5中测试过)如果你想在服务中使用它(或在控制器中),你可以注入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;
}
2w3kk1z5

2w3kk1z53#

$this['imagine']的使用似乎仅限于在PHP模板中使用它。如果在控制器或服务中使用它,您可以将其作为服务使用,或者从容器中获取它(使用服务的旧风格),手动将其注入类(在service.yaml文件中,使用@liip_imagine.service.filter),或者使用它提供的class-as-a-service-name,就像您从Request objec,LoggerInterface,或者Symfony 3.3+中的大多数其他东西(从代码来看,它出现在Liip\ImagineBundle\Service\FilterService类中)。

pb3s4cty

pb3s4cty4#

还测试了Symfony 6.3和liip/imagine-bundle 2.11

<?php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;

class ItemController extends AbstractController{
    public function __construct(private readonly CacheManager $imagineCacheManager) {}

    #[Route('/list', name: 'list')]
    public function list(): Response {
        $pathRelative = '/uploads/images/image.png';
        $resolvedPath = $this->imagineCacheManager->generateUrl($pathRelative, 'my_thumb');
        $htmlString = '<img src="'.$resolvedPath.'" class="img-fluid img-thumbnail"   alt="..." >';
        return $this->render('item/list.html.twig', ['htmlString' => $htmlString,]);
    }
}

相关问题