gpt4 book ai didi

php - 使用 Liipimaginebundle 更新和删除缓存图像

转载 作者:可可西里 更新时间:2023-10-31 22:42:51 24 4
gpt4 key购买 nike

我正在使用 liipimaginebundle,除了在更新原始图像时删除和更新缓存图像外,一切正常。我想知道我该怎么做

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

}

最佳答案

这是一个很好的解决方案

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

关于php - 使用 Liipimaginebundle 更新和删除缓存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com