Reputation: 1273
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.
Upvotes: 2
Views: 3431
Reputation: 419
also tested with Symfony 6.3 and 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,]);
}
}
Upvotes: 0
Reputation: 54
(tested in Symfony 5) if you want to use it in service for intance (or in controller) you could inject the Liip\ImagineBundle\Imagine\Cache\CacheManager and use it within your class:
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;
}
Upvotes: 3
Reputation: 35169
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).
Upvotes: 1
Reputation: 1401
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
Upvotes: 3