Code snippets for symfony 1.x

Navigation

Upload Image and generate thumbnails with Admin Generator

With admin generator you can have a field specified to be a uploading file. Then as it is written in the doc, you can wirte in your generator.yml :

      picture:             
        name: Picture
        type: admin_input_file_tag
        upload_dir: picture
        params: include_link=picture include_remove=true

Then the file will be uploaded in /upload/picture directory

But maybe you want to restrict the size of the picture or add different files for different picture sizes.

Then this is an easy way to generate thumbnails in subdirectories of the main upload directory specified.

Just add the following method to your action class and adapt :

action.class.php:

  protected function updateProductFromRequest()
  {
    $product = $this->getRequestParameter('product');
 
    $thumbnails[]=array('dir' => '16x16', 'width' => 16, 'height' => 16);
    $thumbnails[]=array('dir' => '32x32', 'width' => 32, 'height' => 32);
 
    if (!$this->getRequest()->hasErrors() && isset($produit['picture_remove']))
    {
      foreach ($thumbnails as $thumbParam)
      {
        $currentFile = sfConfig::get('sf_upload_dir').'/picture/'.$thumbParam['dir'].'/'.$this->produit->getPhoto();
        if (is_file($currentFile)) unlink($currentFile);
      }
    }
 
    parent::updateProductFromRequest();
 
    if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('product[picture]'))
    {
      $fileName=$this->product->getPicture();
      foreach ($thumbnails as $thumbParam)
      {
        $thumbnail = new sfThumbnail($thumbParam['width'], $thumbParam['height'],true,false); 
        $thumbnail->loadFile(sfConfig::get('sf_upload_dir')."/product/".$fileName);
        $thumbnail->save(sfConfig::get('sf_upload_dir').'/product/'.$thumbParam['dir'].'/'.$fileName, 'image/jpeg');
      }
    }
  }

As for uploaded files, fenerated thumbnails are not deleted when record is deleted.

by Sylvain PAPET on 2006-10-16, tagged admin  generator  thumbnail 

Comments on this snippet

gravatar icon
#1 robert speer on 2007-03-09 at 01:48

you'll need to add your customizations to the field under params, fields, then the name of the column

generator:
  class:              sfPropelAdminGenerator
  param:
    model_class:      Agents
    theme:            default
    fields:
       NAME_OF_FIELD_IN_TABLE:             
        name: NAME_TO_DISPLAY_IN_CRUD
        type: admin_input_file_tag
        upload_dir: picDIRECTORY
        params: include_link=picDIRECTORY include_remove=true
 
 
 
 
a gotcha is the indention (remember no tabs)
 
fields should be indented under param, and the name of the field under that
 
good snippet, thanks for your help
gravatar icon
#2 Robert Speer on 2007-03-12 at 08:20
protected function updateAgentsFromRequest()
  {/*RHS 3/12/07 this is an adaptation of a snippet from the symfony sippet library written by Sylvain PAPET
      @ http://www.symfony-project.com/snippets/snippet/101
      This overrides the function of the same name in the automaticly generated action.class.php (aka the parent function)
 
      WHERE: 
           table = agents
           upload subdirectory = agent_pics
           image field name = photo_url (camelcasee to PhotoUrl)
 
          * whenever you see the agents, agent_pics, photo_url, or PhotoUrl that probably means you need to change that to your data
            unless you're working on a database of secret agents :) 
 
        You'll need the sfThumbnails plugin for this
        http://www.symfony-project.com/trac/wiki/sfThumbnailPlugin
 
        You'll also need to create and set the correct permissions for you thumbnails directories
          (in this example uploads/agen_pics, uploads/agen_pics/90x90, & uploads/agen_pics/200x200)
      */
 
      //get the form variables
    $agents = $this->getRequestParameter('agents');
 
    // 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);
 
    // IF there are no errors in the form AND the remove picture flag is set OR a new picture is being added
    // erase the associated thumnail images (garbage collection)
    // I made a change from the original snippet by adding the bit where the thumbails are cleaned out if a new image is entered into the form
    if (!$this->getRequest()->hasErrors() && (isset($agents['photo_url_remove']) OR $this->getRequest()->getFileSize('agents[photo_url]')))
    {
      foreach ($thumbnails as $thumbParam)
      {
        $currentFile = sfConfig::get('sf_upload_dir').'/agent_pics/'.$thumbParam['dir'].'/'.$this->agents->getPhotoUrl();
        if (is_file($currentFile)) unlink($currentFile);
      }
    }
 
    /* This is the awsome part, this line calls the original version of this same function.
       This means that the original generated function can change and this function does not need to be edited, 
       so it's less work now, and less work in the future, AND less surprises.
       It's called overriding functions learn it/love it */
    parent::updateAgentsFromRequest();
 
    //IF there are no errors in the form AND the photo exists
    // create a thumbnail in the directory & size defined int $thumbnails
    if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('agents[photo_url]'))
    {
      $fileName=$this->agents->getPhotoUrl();
      foreach ($thumbnails as $thumbParam)
      {
        $thumbnail = new sfThumbnail($thumbParam['width'], $thumbParam['height'],true,false); 
        $thumbnail->loadFile(sfConfig::get('sf_upload_dir')."/agent_pics/".$fileName);
        $thumbnail->save(sfConfig::get('sf_upload_dir').'/agent_pics/'.$thumbParam['dir'].'/'.$fileName, 'image/jpeg');
      }
    }
  }
gravatar icon
#3 Christoph Christ on 2009-05-07 at 08:19

Hi friends of new symfony 1.2

As you probably might have mentioned, sdymfony 1.2 contains an all new and shiny admin generator - a complete rewrite. This is a milestone, as it supports now even better programming methods ans techniques. Thanks Fabier :)

Now the tiny little feature described above, how to generate the thumbnails stopped working, because this method does not existing anymore.

To get admin generator uploads to create also thumbnails you need to enhance your Form script lib/form/YourclassForm.php with the following code:

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 ($filename != "") { // 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;
 

}

Have fun :) Christoph

gravatar icon
#4 Christoph Christ on 2009-05-07 at 08:24

Sorry, I've messed up somehow the code - here is the snippet again :)

<?php 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 ($filename != "") { // 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(&#039;sf_upload_dir&#039;).&#039;/pictures/hotel/&#039;.$thumbParam[&#039;dir&#039;].&#039;/&#039;. $fn;
      if (is_file($currentFile)) unlink($currentFile);
    }
    foreach ($thumbnails as $thumbParam)
    {
        $thumbnail = new sfThumbnail($thumbParam[&#039;width&#039;], $thumbParam[&#039;height&#039;],true,false); 
        $thumbnail-&gt;loadFile(sfConfig::get(&#039;sf_upload_dir&#039;).&quot;/pictures/hotel/&quot;.$fn);
        $thumbnail-&gt;save(sfConfig::get(&#039;sf_upload_dir&#039;).&#039;/pictures/hotel/&#039;.$thumbParam[&#039;dir&#039;].&#039;/&#039;.$fn, &#039;image/jpeg&#039;);
    }
}
// do not forget to return the value of the parent-function, otherwise it stops working
return $fn;
 

}
?>

gravatar icon
#5 Christoph Christ on 2009-05-07 at 08:25

[CODE] 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 ($filename != "") { // 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(&#039;sf_upload_dir&#039;).&#039;/pictures/hotel/&#039;.$thumbParam[&#039;dir&#039;].&#039;/&#039;. $fn;
      if (is_file($currentFile)) unlink($currentFile);
    }
    foreach ($thumbnails as $thumbParam)
    {
        $thumbnail = new sfThumbnail($thumbParam[&#039;width&#039;], $thumbParam[&#039;height&#039;],true,false); 
        $thumbnail-&gt;loadFile(sfConfig::get(&#039;sf_upload_dir&#039;).&quot;/pictures/hotel/&quot;.$fn);
        $thumbnail-&gt;save(sfConfig::get(&#039;sf_upload_dir&#039;).&#039;/pictures/hotel/&#039;.$thumbParam[&#039;dir&#039;].&#039;/&#039;.$fn, &#039;image/jpeg&#039;);
    }
}
// do not forget to return the value of the parent-function, otherwise it stops working
return $fn;
 

}

[/CODE]

gravatar icon
#6 Robin Kanters on 2010-11-01 at 05:11

Typo on the first line ("write")