Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. LiipImagineBundle
  5. Events

Events

Edit this page

Events availables in the bundle are PRE_RESOLVE and POST_RESOLVE. Both receive a CacheResolveEvent as event.

PRE_RESOLVE

Called before url to the cached filtered image is generated.

POST_RESOLVE

Called after url to the cached filtered image is generated.

Example: Signed URLs

Here is an implementation example about users media with differents filters and an S3 as IONOS. At the end we will update the url to get a temporary signed url to a private resource:

First, we need to configure the adapters and the filesystem where will be loaded the images. Normally no need to configure where the cached images will be stored, but we will need it in the next step to generate a signed url.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#oneup_flysystem.yaml

oneup_flysystem:
    adapters:
        # Original image addapter
        user_adapter:
            awss3v3:
                client: Aws\S3\S3Client
                bucket: '%env(IONOS_S3_BUCKET_NAME)%'
                prefix: "users" # Original image location

        # One adapter per filter and the location of the generated images, with the cache_prefix
        user_thumbnail_adapter:
            awss3v3:
                client: Aws\S3\S3Client
                bucket: '%env(IONOS_S3_BUCKET_NAME)%'
                prefix: "cache/user_thumbnail"
        user_medium_adapter:
            awss3v3:
                client: Aws\S3\S3Client
                bucket: '%env(IONOS_S3_BUCKET_NAME)%'
                prefix: "cache/user_medium"

    filesystems:
        user:
            adapter: user_adapter
            mount: user
        userThumbnail:
            adapter: user_thumbnail_adapter
            mount: userThumbnail
        userMedium:
            adapter: user_medium_adapter
            mount: userMedium

To get a cached resource as private we need to configure the acl of the resolver to private, or the generated image will be in public, it's not what we want in this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
liip_imagine:
    driver: "gd"
    loaders:
        user_loader:
            flysystem:
                filesystem_service: oneup_flysystem.user_filesystem

    resolvers:
        aws_s3_resolver:
            aws_s3:
                bucket: '%env(IONOS_S3_BUCKET_NAME)%'
                client_config:
                    credentials:
                        key: '%env(IONOS_S3_ACCESS_ID)%'
                        secret: '%env(IONOS_S3_ACCESS_SECRET)%'
                    endpoint: '%env(IONOS_S3_ENDPOINT)%'
                    region: '%env(IONOS_S3_REGION)%'
                    version: '%env(IONOS_S3_VERSION)%'
                acl: private
                cache_prefix: cache

                get_options:
                    Scheme: 'https'
                put_options:
                    CacheControl: 'max-age=86400'
    cache: aws_s3_resolver
        filter_sets:
            cache: ~
            user_thumbnail:
                cache: aws_s3_resolver
                quality: 75
                filters:
                    thumbnail: { size: [ 130, 130 ], mode: outbound }
                data_loader: user_loader
            user_medium:
                cache: aws_s3_resolver
                quality: 75
                filters:
                    thumbnail: { size: [ 302, 180 ], mode: outbound }
                data_loader: user_loader

Finally we create a post resolve subscriber to update the url to the private resource location.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namespace App\EventSubscriber;

use App\Enum\MediaFilterEnum;
use App\Repository\MediaRepository;
use League\Flysystem\FilesystemOperator;
use Liip\ImagineBundle\Events\CacheResolveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LiipImagineFilterSubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly FilesystemOperator $userThumbnailFilesystem,
        private readonly FilesystemOperator $userMediumFilesystem
    )
    {
    }

    public function onPostResolve(CacheResolveEvent $event): void
    {
        $path = $event->getPath();
        $filter = $event->getFilter();

        $date = new \DateTime();
        // We set the expiration in 10 minutes for example.
        $date = $date->add(new \DateInterval('PT10M'));

        if ($filter === MediaFilterEnum::USER_THUMBNAIL->value) {
                $url = $this->userThumbnailFilesystem->temporaryUrl($path, $date);
        }
        else if ($filter === MediaFilterEnum::USER_MEDIUM->value) {
                $url = $this->userMediumFilesystem->temporaryUrl($path, $date);
        }

        if (isset($url)) {
            $event->setUrl($url);
        }
    }

    public static function getSubscribedEvents(): array
    {
        return [
            'liip_imagine.post_resolve' => 'onPostResolve'
        ];
    }
}

Now, you will get a proper signed url to get your private resource.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Get your Symfony expertise recognized

    Get your Symfony expertise recognized

    Version:

    Table of Contents

    • PRE_RESOLVE
    • POST_RESOLVE
    • Example: Signed URLs

    Symfony footer

    Avatar of Ilia Lazarev, a Symfony contributor

    Thanks Ilia Lazarev (@ilzrv) for being a Symfony contributor

    1 commit • 2 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony