Automatic deletion of thumbnails created by sfImageTransformExtraPlugin when deleting the original image in a symfony admin generator based backend

Flattr this
Tweet this: Automatic deletion of thumbnails created by sfImageTransformExtraPlugin when deleting the original image in a symfony admin generator based backend
Dent this: Automatic deletion of thumbnails created by sfImageTransformExtraPlugin when deleting the original image in a symfony admin generator based backend
Datum: 15.08.2010 12:34:13

Symfony offers a fast and easy way for setting up admin backends including support for image uploads. You only need to change the fields widget and validator in the Form Class and symfony handles the rest:

/lib/Form/doctrine/%Tablename%Form.class.php
  public function configure()
  {
    $this->widgetSchema['image'] = new sfWidgetFormInputFileEditable(array(
      'label' => 'Field Name',
      'file_src' => '/uploads/images/'.$this->getObject()->getImage(),
      'is_image' => true,
      'edit_mode' => !$this->isNew(),
      'template' => '%file% %input% %delete% %delete_label%'
    ));
    
    $this->validatorSchema['image'] = new sfValidatorFile(array(
      'required'   => false,
      'mime_types' => 'web_images',
      'path'       => sfConfig::get('sf_upload_dir').'/images',
    ));
    
    // delete checkbox
    $this->validatorSchema['image_delete'] = new sfValidatorPass();
  }

Combining that with the power of sfImageTransformExtraPlugin to create thumbnails of images based on profiles is really great:

  • No need to handle the thumbnail generation for yourself.
  • Easy automatic creation on first usage
  • No need for extra batch script needed if you ever choose to change the thumbnail sizes.

Big thx to Stuart Lowes for sfImageTransformPlugin and Christian Schäfer for sfImageTransformExtraPlugin.

The creation of the thumbnails is taken care of now, but what about deleting thumbnails if their base image is deleted ? We need to handle that ourselfs.

My first thought was to set up a cronjob to handle that task.
It could scan the upload directory and delete all images from the thumbnail directory which don't have a corresponding one in the upload directory. A perfect task for sfFinder and sfFilesystem. But that would mean each project would have to setup a cronjob and there would always be a delay until the files are really deleted. That was not what I wanted.

Since I am new to symfony and its admin generation, I had to understand what happens behind the curtain first. What came up with is an easy solution extending the BaseFormDoctrine class by overwriting the removeFile() method.

/lib/form/doctrine/BaseFormDoctrine.class.php
abstract class BaseFormDoctrine extends sfFormDoctrine
{
  public function setup()
  {
  }
  
  /**
   * Removes the uploaded image AND all thumbnails created by sfImageTransformExtraPlugin in %sf_root_dir%/web/thumbnails/
   */
  protected function removeFile($field)
  {
    if (!$this->validatorSchema[$field] instanceof sfValidatorFile)
    {
      throw new LogicException(sprintf('You cannot remove the current file for field "%s" as the field is not a file.', $field));
    }

    $directory = $this->validatorSchema[$field]->getOption('path');
    if ($directory && is_file($file = $directory.'/'.$this->getObject()->$field))
    {
      $finder = sfFinder::type('file');
      /* @var $finder sfFinder */
      $finder->name($this->getObject()->$field);
      $files = $finder->in(sfConfig::get('sf_root_dir') . '/web/thumbnails/');
      $filesystem = new sfFilesystem();
      $filesystem->remove($files);
    }

    parent::removeFile($field);
  }
}

It usessfFinder, which searches all files with the same filename as the file in the thumbnails directory and then deletes them using sfFileSystem->remove().
Pretty simple.

It's always great to see what you can achieve in symfony with only some minor changes.


Trackbacks (0)

Trackbackurl: http://www.robo47.net/trackback/blogentry/202

Es sind keine Trackbacks vorhanden.


Kommentare (1)

Die Kommentare zu diesem Beitrag sind gesperrt.

You liked it ? Link it on your homepage or blog: