使用 Liipimaginebundle 更新和删除缓存图像 [英] Update and remove cached image using Liipimaginebundle

查看:29
本文介绍了使用 Liipimaginebundle 更新和删除缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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}

这是实体:图像

/**
 * 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();
}

}

推荐答案

这是一个很好的解决方案

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 }

创建监听器

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());
    }
}
}

这篇关于使用 Liipimaginebundle 更新和删除缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆