![]() |
|
Snippets |
|
This helper return path to a generate (and cached) thumbnail for an image and sizes given.
First it checks if the thumbnail as ever been created for the given size. If not, it created it and return the path to the thumbnail. Else, it will only return the path to the thumbnail.
Thumbnails are stored in a sub-directory of the original image named like [width]x[height].
examples : product/foobar.jpg which is 640x480 images - getThumbnail (320x320) will the first time generate the thumbnail "product/320x320/foobar.jpg", and return the path to this image.
parameters : - $image_path should be the path and filename of the image under uploads directory. ex: product/foobar.jpg - $width is the maximal thumbnail width - $height is the maximal thumbnail height
function getThumbnail($image,$width=null,$height=null, $scale = true, $inflate = true, $quality = 75) { $image_dir=dirname($image); $image_file=basename($image); $thumbnail_dir=''; if ($width>0) $thumbnail_dir.=$width; if ($height>0) $thumbnail_dir.='x'.$height; if ($width>0 || $height>0) $thumbnail_dir.='/'; if (!file_exists(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir.$image_file) && ($width!=null || $height!=null)) { if (!is_dir(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir)) { mkdir (sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir,0777); } $thumbnail = new sfThumbnail($width, $height,$scale,$inflate,$quality); $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$image_file); $thumbnail->save(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir.$image_file); } return '/uploads'.'/'.$image_dir.'/'.$thumbnail_dir.$image_file; }
This is very usefull to use it in model like this :
In model :
public function getThumbnail($width=null,$height=null) { sfLoader::loadHelpers('Thumbnail'); return getThumbnail('product/'.$this->getImage(),$width,$height); }
in template :
<?php foreach ($products as $product) { ?> <li><?php echo image_tag($product->getThumbnail(150,100)); ?></li> <?php } ?>
limitation : - work on images of upload directories only (other dir might have permission problem) - the check of thumbnail existance don't take care of scale and inflate values. It is easy to update the code to stored thumbnail in different subdirectory according to these parameters.
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.