Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update and remove cached image using Liipimaginebundle

I'm using liipimaginebundle and everything work fine except for deletion and update cached images when I update the original image. I would to know How can I do this

config.yml

liip_imagine:
resolvers:
   default:
      web_path: ~

filter_sets:
    cache: ~
    travel_75_55:
        quality: 80
        filters:
            thumbnail: { size: [75, 55], mode: outbound }

    travel_270_161:
        quality: 75
        filters:
            thumbnail: { size: [270, 161], mode: outbound}

And this is Entity: Image

/**
 * Image
 *
 * @ORM\HasLifecycleCallbacks
*/
class Image
{
//.................

public $file;


public function setFile($file)
{
    $this->file = $file;

    if (null !== $this->url) {
    $this->tempFilename = $this->url;

    $this->url = null;
    $this->alt = null;
    }
}

public function getFile()
{
    return $this->file;
}

private $tempFilename;

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
    if (null === $this->file) {
    return;
    }

$this->url = $this->file->guessExtension();

$this->alt = $this->file->getClientOriginalName();
}

/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
    if (null === $this->file) {
    return;
    }

    if (null !== $this->tempFilename) {
    $oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;

        if (file_exists($oldFile)) {
            unlink($oldFile);
        }
    }

    $this->file->move(
    $this->getUploadRootDir(), 
    $this->id.'.'.$this->url
    );
}

/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
    $this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
}

/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
    if (file_exists($this->tempFilename)) {
    unlink($this->tempFilename);
    }
}

public function getUploadDir()
{
    return 'uploads/img/travels';
}

protected function getUploadRootDir()
{
    return __DIR__.'/../../../../web/'.$this->getUploadDir();
}

public function getWebPath()
{
    return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl();
}

}
like image 617
hous Avatar asked Dec 17 '14 11:12

hous


1 Answers

Here is a solution that works fine

services:
    project.cacheimage_listener:
        class: Project\DashboardBundle\Listener\CacheImageListener
        arguments: ["@liip_imagine.cache.manager"]
        tags:
            - { name: doctrine.event_listener, event: postUpdate }
            - { name: doctrine.event_listener, event: preRemove }

create the listener

namespace Project\DashboardBundle\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Project\DashboardBundle\Entity\Image;

class CacheImageListener
{
protected $cacheManager;

public function __construct($cacheManager)
{
    $this->cacheManager = $cacheManager;
}

public function postUpdate(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    if ($entity instanceof Image) {
        // clear cache of thumbnail
        $this->cacheManager->remove($entity->getUploadDir());
    }
}

// when delete entity so remove all thumbnails related 
public function preRemove(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    if ($entity instanceof Image) {

        $this->cacheManager->remove($entity->getWebPath());
    }
}
}
like image 111
hous Avatar answered Oct 20 '22 05:10

hous