whiteletters in blankpapers whiteletters in blankpapers - 2 years ago 189
PHP Question

Store both original and filtered image under the same folder - LiipImagineBundle

I am using LiipImagineBundle to create a filtered image from an uploaded image. The current configuration of the bundle is:

web_path: ~

cache: ~
relative_resize: { heighten: 66 }

My Goal is to create a directory structure like:


The type is either
for small image or
for original size. The subdirectory is created from upload date.

The problem is that LiipImagineBundle stores the filtered images in

How can I omit the
part of the url? I need each uploaded image and its filtered to be stored on the same final folder.

I am not an expert and I did a lot of research to no avail. The configuration allows to change the web path of cache either for the whole filters or for individual filters but the filter name always apears in the final url.

Your help is much appreciated.

Answer Source

I used the services provided with LiipImagineBundle to have more control on the filtering process. In other words, instead of getting liip_imagine.controller service inside controller, I used the "underlying" services : liip_imagine.data.manager, liip_imagine.filter.manager and liip_imagine.cache.manager.

In my special situation, I omitted also the use of liip_imagine.cache.manager because I finally decided to store the filtered images in the same directory as the original images. So cache is not involved (neither in configuration nor in direcrory strucure). So everything is stored now in web\images

    $small_path= 's_'. $image_file->getClientOriginalName();

    $filter = 'my_filter';
    $config= array( 'widen' => 960) ;

    $path = $image->getWebPath();

    $container = $this->container;                                 
    $dataManager = $container->get('liip_imagine.data.manager');
    $filterManager = $container->get('liip_imagine.filter.manager');

    $binary = $dataManager->find($filter, $path);

    $filteredBinary =  $filterManager->applyFilter( $binary, $filter, array(
                           'filters' => array(
                               'relative_resize' =>  $config

    $small_path = str_replace("subdirectory/","subdirectory/s_",'subdirectory/'.$small_path );
    $s = $sBinary->getContent();                               

    $f = fopen($smallPath, 'w');                                 
    fwrite($f, $s );                                            

Of course, this invloves a change in configuration:

            relative_resize: [] 

In fact, I can now use dynamically any configuration of relative_resize built-in filter. Thank you for your time. and I hope I receive feedback regarding this approach.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download