Search code examples
phpsymfonyliipimaginebundle

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:

liip_imagine:
    resolvers:
       default:
          web_path: ~

    filter_sets:
        cache: ~
        my_filter:
            filters:
                relative_resize: { heighten: 66 } 

My Goal is to create a directory structure like:

web_root\{subdirectory}\{type}_{filename_md5_hash}.jpeg

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

The problem is that LiipImagineBundle stores the filtered images in web_root\my_filter\{subdirecory}\{type}_{filename_md5_hash}.jpeg.

How can I omit the my_filter 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.


Solution

  • 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 );                                            
        fclose($f);
    

    Of course, this invloves a change in configuration:

        my_filter:
            filters:
                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.