【问题标题】:YII IMAGE CACHEYII 图像缓存
【发布时间】:2013-04-18 03:59:40
【问题描述】:

我正在使用 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 的图片。新图片正在使用新的缓存文件夹进行更新,并且存在以前的缓存文件夹。

【问题讨论】:

  • 能举个代码例子吗?
  • $img = $image_name[0]['image_name']; $p = '图像/'.$img; $newpath = Yii::app()->iwi->load($p)->resize(100,300,Image::AUTO)->cache(); $newpath = explode('=',$newpath); ?> 图片:

    labelEx($model, 'image'); ?> fileField($model, 'image'); ?> error($model, 'image'); ?>
    当我更新特定图片时。假设我正在更新一张表中 id 为 1 的图片。新图片正在使用新的缓存文件夹进行更新,并且之前的缓存文件夹存在。
  • 我不确定我是否理解。预期的行为是什么?你是说它使用过去的缓存而不是新的图像?
  • 没有。当我更新特定图像时,前一个图像的前一个缓存文件夹保留在服务器上。我想在更新特定图像时删除前一个图像的前一个缓存文件夹。
  • 在下面查看我的答案。不幸的是,默认情况下你想要做的事情是不可能的,因为用于命名文件/文件夹的哈希使用时间元素(这意味着它可以是任何东西,所以如果没有你覆盖的原始文件,你将无法再次找到它)

标签: php yii yii-extensions


【解决方案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);
    }

}

希望这将使您走上正轨。祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-01
    • 2013-10-24
    • 2014-03-17
    • 2010-12-29
    • 2016-10-23
    • 2015-06-27
    • 1970-01-01
    • 2014-08-16
    相关资源
    最近更新 更多