Here's a function I wrote to flush all derivative images for a given file. I call it from the ImageField module when deleting an image, so that I don't get left with unneeded derivatives.
/**
* Flush cached media only for given file
* @param file
* The file for which to flush cached media
*/
function _imagecache_file_flush($file) {
$presets = _imagecache_get_presets();
foreach ($presets as $preset) {
$filepath = imagecache_create_path($preset, $file['filepath']);
if (is_file($filepath)) {
@unlink($filepath);
// Remove all empty parent directories.
$patharray = explode("/", dirname($filepath));
$pathlength = count($patharray);
$foundfiles = false;
// Remember that we never want the leading "files" dir to be deleted!
for ($i = $pathlength; !$foundfiles && $i > 1; $i--) {
$currentdir = implode("/", array_slice($patharray, 0, $i));
$filesindir = glob($currentdir. "/*");
if (count($filesindir) == 0) {
@rmdir($currentdir);
} else {
$foundfiles = true;
}
}
}
}
drupal_set_message(t('Flushed Preset Images for file: @file',
array('@file' => $file['filepath'])));
}
In ImageField, wherever the line file_delete($file['filepath']); occurs, I've inserted the following above:
if (function_exists(_imagecache_file_flush)) {
_imagecache_file_flush($file);
}
I do apologise for not providing a patch. Just thought I'd share my code.
Comments
Comment #1
dopry commentedI won't be adding this feature to imagecache 1.x. All new development is in the 2.x branch for the time being until I start working on the DRUPAL-6 port. It is however already in 2.x.
Comment #2
ciberwing commentedHello, how do i can use it in my site?
i don't know where i must put the "plugin"..
some hel please
Comment #3
mottolini commentedI'm using imagecache 2 and now there is a function that does it: imagecache_image_flush($path)