![]() |
|
Snippets |
|
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.
Comments on this snippet
you'll need to add your customizations to the field under params, fields, then the name of the column
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);
}
Have fun :) Christoph
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);
}
?>
[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);
}
[/CODE]