Liipimaginebundle: imagine_filter() twig function output an absolute URL

Created on 29 Jul 2015  路  5Comments  路  Source: liip/LiipImagineBundle

Hi,

Previously, my project uses Avalanche123/AvalancheImagineBundle where the _imagine_filter()_ was _apply_filter()_.
The old _apply_filter()_ function outputs by default a relative path of the cached image. That was a good option for me because I used this bundle in CLI too. And absolute URL generated from CLI starts with http://localhost. That's not what I want.

Then my question is : Is there a way to outputs a relative path with _imagine_filter()_ function from a twig template ?

Most helpful comment

As @makasim suggested,

You can try to add a post resolve listener which convert absolute url to relative.

The code below, might turn useful to someone using a simpler cdn like keycdn:

My _EventListener/CacheResolveListener_:

<?php
namespace AppBundle\EventListener;

use Liip\ImagineBundle\Events\CacheResolveEvent;

class CacheResolveListener
{
    function onLiipimaginePostresolve(CacheResolveEvent $e)
    {
        $e->setUrl(parse_url($e->getUrl())['path']);
    }
}

My _config/services.yml_:

...
  app.event_listener.post_cache_resolver:
      class: AppBundle\EventListener\CacheResolveListener
      tags:
          - { name: kernel.event_listener, event: liip_imagine.post_resolve }
...

Then in twig I use it as:

{% set assetPackage = 'keycdn' %}
{{ asset('assets/media/logo.png' | imagine_filter('thumb'), assetPackage) }}

All 5 comments

Then my question is : Is there a way to outputs a relative path with imagine_filter() function from a twig template ?

The absolute is returned on purpose to keep resolvers consistent. Relative url is an edge case.

There is not easy way to enable relative urls. You can try to add a post resolve listener which convert absolute url to relative.

And absolute URL generated from CLI starts with http://localhost. That's not what I want.

You can configure router context parameters host,port etc to get correct absolute url.

Thanks for the quick answer @makasim !

You're right I can solve my problem by configuring the router context in my app parameters.

parameters:
    router.request_context.host: mywebsite.com

@makasim because imagine_filter returns absolute url it is not possible to wrap result into asset
so framework.assets.base_urls: ['http://cdn.example.com', 'http://cdn2.example.com'] is ignored

@mitrae you can do achieve it while configuring the resolver

As @makasim suggested,

You can try to add a post resolve listener which convert absolute url to relative.

The code below, might turn useful to someone using a simpler cdn like keycdn:

My _EventListener/CacheResolveListener_:

<?php
namespace AppBundle\EventListener;

use Liip\ImagineBundle\Events\CacheResolveEvent;

class CacheResolveListener
{
    function onLiipimaginePostresolve(CacheResolveEvent $e)
    {
        $e->setUrl(parse_url($e->getUrl())['path']);
    }
}

My _config/services.yml_:

...
  app.event_listener.post_cache_resolver:
      class: AppBundle\EventListener\CacheResolveListener
      tags:
          - { name: kernel.event_listener, event: liip_imagine.post_resolve }
...

Then in twig I use it as:

{% set assetPackage = 'keycdn' %}
{{ asset('assets/media/logo.png' | imagine_filter('thumb'), assetPackage) }}
Was this page helpful?
0 / 5 - 0 ratings