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

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

我希望能够上传一个文件,并从它创建3缩略图和存储在S3服务器上的一切。
我的liip/LiipImagineBundle设置如下:
liip_imagine:

  1. # configure resolvers
  2. resolvers :
  3. # setup the default resolver
  4. default :
  5. # use the default web path
  6. web_path : ~
  7. # your filter sets are defined here
  8. filter_sets :
  9. # use the default cache configuration
  10. cache : ~
  11. # the name of the "filter set"
  12. my_thumb :
  13. # adjust the image quality to 75%
  14. quality : 75
  15. # list of transformations to apply (the "filters")
  16. filters :
  17. # create a thumbnail: set size to 120x90 and use the "outbound" mode
  18. # to crop the image when the size ratio of the input differs
  19. thumbnail : { size : [120, 90], mode : outbound }
  20. # create a 2px black border: center the thumbnail on a black background
  21. # 4px larger to create a 2px border around the final image
  22. background : { size : [124, 94], position : center, color : '#000000' }
  23. # the name of the "filter set"
  24. thumb_square :
  25. # adjust the image quality to 75%
  26. quality : 75
  27. # list of transformations to apply (the "filters")
  28. filters :
  29. thumbnail : { size : [300, 300], mode : outbound }
  30. # create a 2px black border: center the thumbnail on a black background
  31. # 4px larger to create a 2px border around the final image
  32. background : { size : [304, 304], position : center, color : '#000000' }
  33. # the name of the "filter set"
  34. thumb_rectangle_md :
  35. # adjust the image quality to 75%
  36. quality : 75
  37. # list of transformations to apply (the "filters")
  38. filters :
  39. thumbnail : { size : [670, 400], mode : outbound }
  40. # create a 2px black border: center the thumbnail on a black background
  41. # 4px larger to create a 2px border around the final image
  42. background : { size : [674, 404], position : center, color : '#000000' }
  43. # the name of the "filter set"
  44. thumb_hd :
  45. # adjust the image quality to 75%
  46. quality : 75
  47. # list of transformations to apply (the "filters")
  48. filters :
  49. thumbnail : { size : [1920, 1080], mode : outbound }
  50. # create a 2px black border: center the thumbnail on a black background
  51. # 4px larger to create a 2px border around the final image
  52. 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缓存管理器。

  1. /** @var CacheManager */
  2. $imagineCacheManager = $this->get('liip_imagine.cache.manager'); // gets the service from the container
  3. /** @var string */
  4. $resolvedPath = $imagineCacheManager->getBrowserPath('/relative/path/to/image.jpg', 'my_thumb'); // resolves the stored path
0wi1tuuw

0wi1tuuw2#

(在Symfony 5中测试过)如果你想在服务中使用它(或在控制器中),你可以注入Liip\ImagineBundle\Imagine\Cache\CacheManager并在你的类中使用它:

  1. public function __construct(UploaderHelper $uploaderHelper, CacheManager $cacheManager)
  2. {
  3. $this->uploaderHelper = $uploaderHelper;
  4. $this->cacheManager = $cacheManager;
  5. }
  6. public function getImageLink($image)
  7. {
  8. $imagePath = $this->uploaderHelper->asset($image, 'imageFile');
  9. $imageLink = $this->cacheManager->getBrowserPath($imagePath, $imagine_filter);
  10. return $imageLink;
  11. }
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

  1. <?php
  2. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  5. class ItemController extends AbstractController{
  6. public function __construct(private readonly CacheManager $imagineCacheManager) {}
  7. #[Route('/list', name: 'list')]
  8. public function list(): Response {
  9. $pathRelative = '/uploads/images/image.png';
  10. $resolvedPath = $this->imagineCacheManager->generateUrl($pathRelative, 'my_thumb');
  11. $htmlString = '<img src="'.$resolvedPath.'" class="img-fluid img-thumbnail" alt="..." >';
  12. return $this->render('item/list.html.twig', ['htmlString' => $htmlString,]);
  13. }
  14. }
展开查看全部

相关问题