Code snippets for symfony 1.x

Navigation

admin Generator and Thumnail generation in file uploads with symfony 1.2

Hello to the desperate,

As you might have probably already mentioned, the old way, how to produce thumbnails stopped working. This is because of the brand new admin generator, using an all new model based on forms, which doesn't support the old methods.

this is the new way, how to get it working again:

put this into your generator.yml file (take care for the correct identation, do not use tabs!!):

      edit:
        title: Photo uploads 
        fields:
          foto:
            type:       admin_input_file_tag
            upload_dir: /uploads/pictures/
            params:     include_link=/uploads/pictures/ include_remove=true
 

and then enhance the file lib/form/YourclassForm.php with the following method:

  protected function processUploadedFile($field, $filename = null, $values = null) {
    // first of all do what this is supposed to do
    $fn = parent::processUploadedFile($field, $filename, $values);
    // and now we can finally start doing additional stuff after the upload *hurra*
    if ($fn != "") {
        // if there is a file, that has been saved
        // multidimensional array that defines the sub-directories to store the thumbnails in
        $thumbnails[]=array('dir' => '90x90', 'width' => 90, 'height' => 90);
        $thumbnails[]=array('dir' => '200x200', 'width' => 200, 'height' => 200);
 
        foreach ($thumbnails as $thumbParam)
        {
          $currentFile = sfConfig::get('sf_upload_dir').'/pictures/'.$thumbParam['dir'].'/'. $fn;
          if (is_file($currentFile)) unlink($currentFile);
        }
        foreach ($thumbnails as $thumbParam)
        {
            $thumbnail = new sfThumbnail($thumbParam['width'], $thumbParam['height'],true,false); 
            $thumbnail->loadFile(sfConfig::get('sf_upload_dir')."/pictures/".$fn);
            $thumbnail->save(sfConfig::get('sf_upload_dir').'/pictures/'.$thumbParam['dir'].'/'.$fn, 'image/jpeg');
        }
    }
    // do not forget to return the value of the parent-function, otherwise it stops working
    return $fn;
  }  
 

Hope, you can use that snippet!

lg Christoph

by Christoph Christ on 2009-05-07, tagged admin  file  generator  sfthumbnail  symfony  uploads 

Comments on this snippet

gravatar icon
#1 Jamie Learmonth on 2009-05-09 at 03:36

Why the double loop?

gravatar icon
#2 charline coutin on 2010-01-19 at 05:17

Thank you so much, it is exactly what I was looking for. I created as well the delete part and I changed the modified part as it was not working for me. I post my code here, knowing that it is not very clean, but it might help some people. By the way, I'm working with Symfony-1.4, it may explain why the modification was not working.

In protected function processUploadedFile :

$file = $this->getObject()->getPicture();
// DELETE :
$delete = $this->getValue('picture_delete');
if(isset($delete)) {
      $folder = sfConfig::get('app_upload_folder').sfConfig::get('app_picture_upload_folder');
      $file1 = $folder.sfConfig::get('app_picture_upload_thumbnail').$file;
      $file2 = $folder.sfConfig::get('app_picture_upload_small').$file;
      sfContext::getInstance()->getLogger()->info("toto ".$file2." ".$file3);
      $delete = unlink($file1);
      $delete = unlink($file2);
}
 

//Change in the modification, just replaced $fn by $file foreach ($thumbnails as $thumbParam) { $currentFile = sfConfig::get('app_upload_folder').sfConfig::get('app_picture_upload_folder').$thumbParam['dir'].'/'. $file;