If using imagecache in a scenario where an image may be updated with the same name, imagecache is not aware that the image has changed. For example, in the profile module uploaded images always take the name 'files/pictures/picture-uid'. If the user uploads a new profile picture, imagecache does not update the cache with the new image of the same name.

Two approaches are available: an 'automatic' cleanup of cached images when the the original changes or a 'manual' clearing of the image's cache when needed by the web developer.

The first approach requires a a lot of overhead (database records, md5's of files, checking, etc) and generally would not be viable for a high-volume website. The amount of checking could cripple the site.

So, I suggest a method for approach 2. Imagecache should provide an api for clearing a single image's cache in every preset whenever needed. Here's a function that would accomplish this task:

/**
 * Clear cached versions of a specific file in all presets
 */
function imagecache_flush_image($path) {
  $presets = _imagecache_get_presets();
  foreach($presets as $presetid => $presetname) {
    $preset_image = file_create_path('imagecache/'.$presetname.'/') . file_create_path($path);
    file_delete($preset_image);
  }
}

Once again, great module!

Comments

quicksketch’s picture

Status: Active » Needs review
quicksketch’s picture

A little more documentation:

/**
 * Clear cached versions of a specific file in all presets
 * @param $path
 *   The Drupal file path to the original image
 */
dopry’s picture

Status: Needs review » Fixed

fixed in 4-7 branch....
changed function name to imagecache_image_flush to match imagecache naming conventions.

change the path generation to use the same code as imagecache_cache.

Anonymous’s picture

Status: Fixed » Closed (fixed)