We have a need to clear the Varnish cache when an image is cropped via the imagecrop module. Our users want to see the updated image right away, but since Varnish is caching the image, users still see the old cropping on the image. We are using 6.x-1.x-dev of imagecrop and 6.x-2.0-beta10 of imagecache.

Imagecrop calls imagecache_build_derivative to create the same image with updated cropping. I've tried adding:

// Flush the image from varnish cache.
if (module_exists('varnish')) {
varnish_expire_cache($src);
}

to line 601 of imagecache.module. This works in clearing the varnish cache. However, I'm not sure if this is the best approach. Looking for ideas on how best to clear varnish after cropping an image.

Comments

djbobbydrake’s picture

So now I'm thinking, no, this is not such a great idea. The varnish cache clearing should be done in the imagecrop module.

djbobbydrake’s picture

I was able to solve the varnish issue by using hook_form_alter to call a function on submit before the imageoffsets_submit function is called. My varnish cache clearing function looks like:

function foo_varnish($form, &$form_state) {
  module_load_include('module', 'imagecrop');
  module_load_include('module', 'imagecache');

  // Find the path to the derivative image that we want to flush.
  $file = _imagecrop_file_load($form_state['values']['fid'], '');
  $sql = "SELECT presetname FROM {imagecache_preset} WHERE presetid = %d";
  $preset_name = db_result(db_query($sql, $form_state['values']['presetid']));
  $preset_path = imagecache_create_path($preset_name, $file->filepath);

  // Flush the image from varnish cache.
  if (module_exists('varnish')) {
    module_load_include('module', 'varnish');
    _varnish_terminal_run('url.purge ' . $preset_path);
  }
}
bibo’s picture

Thanks for the patch cchay.

I'm currently using a version of it (patched imagecrop). Hoping to do this in the future with cache_actions + rules though.

rjbrown99’s picture

See the following link for how I solved this same issue. Basically I use filefield_paths with a custom token. This renames the image each time it is saved, so with a new filename the upstream CDN just times out the old one eventually and caches the new file because the name is different. This gets you 90% of the way there, but you need to do imagecache_build_derivative() in the event you want to generate the image yourself.

http://drupal.org/node/1250506#comment-4886368

fizk’s picture

Status: Active » Closed (fixed)

Please reopen if this is still an issue with ImageCache 6.x-2.0-rc1.