繁体   English   中英

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扩展名,我想我明白你在问什么。

不幸的是,您无法做到这一点。 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);
    }

}

希望这将使您走上正确的道路。 祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM