2013-04-16 55 views
0

我在yii框架中使用IWI擴展來緩存圖像。它工作正常,但問題是當我更新圖像的過去的緩存文件和文件夾存在。更新映像後,請幫助我刪除那些過去的緩存文件夾。YII IMAGE CACHE

編輯:

$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的圖像。新圖片正在更新,並且新緩存文件夾和之前的緩存文件夾存在。

+0

你能給一個代碼示例嗎? –

+0

$ img = $ image_name [0] ['image_name']; $ p ='images /'.$ 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的圖像。新圖片正在更新,並且新緩存文件夾和之前的緩存文件夾存在。 – SuperOV

+0

我不確定我是否理解。預期的行爲是什麼? 你是說它使用過去的緩存而不是新的圖像? –

回答

0

好吧,我看看IwI的擴展,我想我明白你問的是什麼。

不幸的是,你不能實現這個開箱即用。 Iwi使用最後修改日期的緩存ID。這意味着更改圖像會創建新的緩存文件/文件夾,而不是根據依賴關係規則進行替換。

我認爲最好的選擇是將延伸Iwi,然後重寫cache()方法(優選使用Yii的緩存)

下面是一個未經測試的示例:

擴展Iwi類: 這是非常basicc並且可能無法涵蓋所有​​的情況(即:用另一個不同的文件覆蓋文件,其中有些是多餘的,但是我這樣做很清楚)

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

} 

希望這會讓你走上正軌。祝你好運