test.ical.ly | getting the web by the balls

Aug/10

17

How to automatically and manually delete thumbnails in symfony with sfImageTransformExtraPlugin

As I promised yesterday there is a new version of sfImageTransformExtraPlugin out which not only allows you to selectively delete generated thumbnails manually using a symfony task but also provides a generic API for doing this automatically using the symfony event dispatcher.

I always planned to implement a generic way of deleting generated images once their source has changed, In fact I already had implemented it halfway through but only Benjamins blog post encouraged me to actually finish it.

I chose to use the symfony event dispatcher and provide an event that you can notify on which the plugin will do the removing. Basically it works just as the removal task but instead of being called by you manually on the command line it is notified by the event that your code issues.

Consider the following route for your thumbnail generation (it’s the default one).

sf_image:
  class: sfImageTransformRoute
  url:   /thumbnails/:format/:filepath.:sf_format
  param: { module: sfImageTransformator, action: index }
  requirements:
    format:    '[\w_-]+'
    filepath:  '[\w/.]+'
    sf_format: 'gif|png|jpg'
    sf_method: [ get ]
  options:
    image_source: File

This route takes three parameters format, filepath and sf_format. The latter isn’t really important as it gets determined by the format configuration anyway so let’s ignore it for this post.

The parameter format is used to identify the target format (i.e. big_teaser, gallery_image, etc..)  that your thumbnail should resemble while the filepath parameter is used to identify the source image.

When your source image change what you want to do is to wipe all files that were generated from the old source no matter what format.

All you have to do to accomplish this is the following in your action, postSave hook or whereever you need.

$dispatcher = ProjectConfiguration::getActive()->getEventDispatcher();
$event = new sfEvent($this, 'sf_image_transform.changed_source', array(
  'route' => 'sf_image',
  'filepath' => '/web/path/to/your/old/source/image.jpg'
));
$dispatcher->notify($event);

The same call without the filepath parameter would wipe all generated files from all sources and if you would provide a format parameter you could wipe only files of a particular format.

Let’s have a last look at a more complicated route used when you maintain your image filepaths in your database using doctrine.

sf_image:
  class: sfImageTransformRoute
  url:   /thumbnails/:type/:format/:path/:slug-:id.:sf_format
  param: { module: sfImageTransformator, action: index, attribute: file }
  requirements:
    format:    '[\w_-]+'
    path:      '[\w/]+'
    slug:      '[\w_-]+'
    id:        '\d+(?:,\d+)?'
    sf_format: 'gif|png|jpg'
    sf_method: [ get ]
  options:
    image_source: Doctrine
    segment_separators: [ '/', '.', '-' ]

Again the most common case is that you want to wipe all files for this particular source.

$dispatcher = ProjectConfiguration::getActive()->getEventDispatcher();
$event = new sfEvent($this, 'sf_image_transform.changed_source', array(
  'route' => 'sf_image',
  'type' => 'GalleryImage',
  'id' => $image->id
));
$dispatcher->notify($event);

You can see that this is actually quite flexible and convenient at the same time.

This week I will also demonstrate how to integrate sfImageTransformExtraPlugin in your administrator backend. And yes I will update the README from the contents of this post. ;)

·



<<

>>

Theme Design by devolux.nh2.me