0

yii フレームワークでの表示時に画像をキャッシュするために IWI 拡張機能を使用しています。正常に動作していますが、問題は、画像を更新するときに過去のキャッシュ ファイルとフォルダーが存在することです。イメージを更新した後、過去のキャッシュ フォルダを削除するのを手伝ってください。

編集:

$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 の画像を更新しているとします。新しい画像は新しいキャッシュ フォルダーで更新され、以前のキャッシュ フォルダーが存在します。

4

1 に答える 1

0

わかりました、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);
    }

}

うまくいけば、それがあなたを正しい軌道に乗せるでしょう。幸運を

于 2013-04-17T19:54:27.890 に答える