Search code examples
phpsymfonyliipimaginebundle

Symfony remove cached images using liipImagine


I'm trying to remove cached images (which are created with LiipImagineBundle) when the source image is deleted or updated. I have already found out that it could be done using CacheManager ( https://github.com/liip/LiipImagineBundle/issues/132 ) The problem is that I can't figure it out how to use it exactly. What else do I need to add (like libraries) to my code despite these three lines:

    $cacheManager = $this->get('liip_imagine.cache.manager');
    $cacheManager->resolve($this->getRequest(),$pngPath,$filter);
    $cacheManager->remove($pngPath, $filter);

I believe there should be something like

    $cacheManager = new CacheManager();

I would really appreciate if anyone could explain me how to do that in more detail.


Solution

  • So, for example in your controller:

    /**
    * Remove an image in the cache based on its relative path and the filter applied to it
    *
    * @param string $path
    * @param string $filter
    *
    * @return void
    */
    protected function removeCachedImageAction($path, $filter)
    {
        $cacheManager = $this->container->get('liip_imagine.cache.manager');
    
        // Remove the cached image corresponding to that path & filter, if it is stored
        if ($cacheManager->isStored($path, $filter)) {
            $cacheManager->remove($path, $filter);
        }
    
    }
    
    /**
    * An action that doesn't do much except testing the function above
    *
    * @param Request $request
    *
    * @return void
    */
    protected function whateverAction(Request $request)
    {
        $path = //... probably from the request
        $filter = //... probably from the request
    
        // Remove the cached image
        $this->removeCachedImage($path, $filter);
    
        // ...
    
    }
    

    As you can see in the CacheManager, the function that you'd like to use is:

    public function remove($paths = null, $filters = null){ ... }
    
    • If $paths is null, the function assumes that you want to remove the cached images for ALL PATHS that have been resolved with the $filters provided.

    • If $filters is null, the function assumes that you want to remove the cached images corresponding for the $paths provided and that have previously been resolved with ALL FILTERS.

    • If $paths and $filters are null, the function assumes that you want to remove the cached images corresponding to ALL PATHS and for ALL FILTERS. Basically ALL CACHED IMAGES.