如何从timthumb的缓存中删除特定文件


How to delete specific files from timthumb's cache?

我有一个照片共享应用程序,我使用timthumb来显示缩略图。当用户删除照片时,我希望缓存的图像也从timthumb的缓存目录中删除以节省空间。目前我只能从缓存中删除所有文件,但这不太理想。

如何在给定原始图像的情况下仅从timthumb的缓存中删除特定文件?

假设我们要删除本地映像的缓存文件:

我们需要的第一件事是 cacheDirectory = './cache'
第二,FILE_CACHE_PREFIX='Timthumb'
那么它是内部的(_int_)
然后有一个 MD5 哈希:

  • 提姆拇指.php的修改时间
  • 字符"-"
  • Timthumb.php的索引
  • 图像的修改时间
  • 调用映像时的 $_SERVER ['QUERY_STRING']
  • "fileCacheVersion",它始终是nubmer 1

最后,FILE_CACHE_SUFFIX = '.timthumb.txt'


例如:

.HTML:

<img src='timthumb.php?src=images/example.png&w=150'>

.PHP:

$cachefile = 'cache/timthumb_int_'.md5(filemtime('timthumb.php') . '-' . fileinode('timthumb.php') . filemtime("images/example.png"). "src=images/example.png&w=150" . 1) . '.timthumb.txt';
unlink($cachefile);

我得出了这个解决方案。我必须对timthumb.php进行少量修改文件,以便我可以包含该文件并从另一个文件实例化类。

提姆拇指.php:

<?php
// If this file is included from other script, don't start timthumb
if (basename($_SERVER['SCRIPT_NAME']) == basename(__FILE__)) {
  timthumb::start();
}
class timthumb {
  ...
  // $cachefile is protected so I create a getter function
  public function getCacheFile() {
    return $this->cachefile;
  }
}

现在我可以获取给定图像的缓存文件名并将其删除。代码不漂亮,但我需要节省空间。

delete_cache.php:

<?php
require 'timthumb.php';
// Fake the query string
$_SERVER['QUERY_STRING'] = 'src=path/to/src/image.jpg&w=200&h=150';
parse_str($_SERVER['QUERY_STRING'], $_GET);
// When instantiated, timthumb will generate some properties: 
// salt, directories, cachefile, etc.
$t = new timthumb();
// Get the cache file name
$f = $t->getCacheFile();
// Delete the file
if (file_exists($f)) {
    unlink($f);
}