Yii图像缓存


YII IMAGE CACHE

我使用IWI扩展在yii框架中显示时缓存图像。它的工作很好,但问题是,当我更新一个图像,过去的缓存文件和文件夹存在。请帮我在更新图像后删除过去的缓存文件夹。

编辑:

$img = $image_name[0]['image_name']; 
$p = 'images/'.$img;
$newpath = Yii::app()->iwi->load($p)->resize(100,300,Image::AUTO)->cache();
$newpath = explode('=',$newpath); ?> 
Image : <br/><br/> 
<?php echo CHtml::image($newpath[1],"image"); ?> 
<div class="row">
<?php echo $form->labelEx($model, 'image'); ?>
<?php echo $form->fileField($model, 'image'); ?>
<?php echo $form->error($model, 'image'); ?>
</div>

当我更新一个特定的图片。假设我正在更新一个在表上id为1的图像。新图片正在更新,新的缓存文件夹和以前的缓存文件夹存在。

好的,我看了一下IwI扩展,我想我明白你问的是什么了。

不幸的是,您无法开箱即用实现此功能。我使用带有最后修改日期的缓存id。这意味着更改映像会创建一个新的缓存文件/文件夹,而不是根据依赖规则进行替换。

我认为最好的选择是扩展Iwi,然后覆盖cache()方法(最好使用Yii的缓存)

下面是一个未测试的例子:

扩展Iwi:这是非常基本的,可能不会涵盖所有情况(例如:用另一个不同的文件覆盖一个文件)。有些是多余的,但我这样做是为了清楚)

class MyIwi extends Iwi
{
    public function cache()
    {
        if (!isset($this->image["file"])) {
            return false;
        } 
        $cacheFolder = YiiBase::getPathOfAlias('webroot.images.site.cache');   
        $imagePath = $this->image["file"];
        $info = pathinfo($imagePath);

        //create unique ID (filename for cached image) using actions and path. then serialize and hash it.
        $needle = $this->actions;
        array_unshift($needle, $imagePath);
        $cachedFilePath = $cacheFolder."/".md5(json_encode($needle)).".".$info["extension"];
        //if cache file doesn't exist or is older than file then cache 
        if(  
           $this->createOrNone() //did not look into this, keeping because it was in original method
           &&
           (!file_exists($cachedFilePath)
           || 
           filetime($imagePath) > filetime($cachedFilePath))
        )
            $this->save($cachedFilePath);
        return Yii::app()->createUrl($cachedFilePath);
    }
}

希望这会让你走上正确的道路。祝你好运