[英]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.