How to add image uploading to your forms with Symfony 1.4 using sfThumbnailPlugin / Cómo agregar carga de imágenes a los formularios con Symfony 1.4 utilizando sfThumbnailPlugin
En este ejemplo mi Form se llama DistributorForm, asi que en la configuración del mismo, inmerso en las otras validaciones coloco:
public function configure()
{
.................
................
// Widget File Editable
$this->widgetSchema['distributor_image'] = new sfWidgetFormInputFile
Editable(array('file_src' => sfConfig::
get('app_directory_distributor').
$this->getObject()->getDistributorImage(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
));
// Validator para el File
$this->validatorSchema['distributor_image'] = new sfValidatorFile
(array(
'required' => false,
'max_size' => 1048576,
'mime_types' => array('image/jpeg','image/pjpeg',
'image/png','image/gif'),
));
$this->validatorSchema['distributor_image']->
setMessage('max_size','The max value is %max_size% Kb.');
$this->validatorSchema['distributor_image']->
setMessage('mime_types','Error mime types %mime_type%.');
...............
................
}
Seguido las funciones doSave y updateObject
protected function doSave ( $con = null )
{
$upload = $this->getValue('distributor_image');
if ( $upload )
{
$filename = 'distributor_'.$this->getObject()->
getIdDistributor().
$upload->getExtension($upload->getOriginalExtension());
$filepath = sfConfig::get('app_directory_distributor').
$filename;
$oldfilepath = sfConfig::get('app_directory_distributor').
$this->getObject()->getDistributorImage();
if (is_file($oldfilepath))
{
unlink($oldfilepath);
}
$thumbnail = new sfThumbnail(sfConfig::
get("mod_distributor_sizes_image_big_width"),
sfConfig::get("mod_distributor_sizes_image_big_height"),
true, true, 75, 'sfGDAdapter');
$thumbnail->loadFile($upload->getTempname());
$thumbnail->save($filepath);
}
return parent::doSave($con);
}
public function updateObject($values = null)
{
$object = parent::updateObject($values);
$object->setDistributorImage(str_replace(
sfConfig::get('app_directory_distributor'), '',
$object->getDistributorImage()));
return $object;
}
Es importante mencionar que si se está creando un nuevo registro, el formato de la imagen se crea erroneo, por lo que debemos modificarlo despues de realiza el registro. Esto lo hacemos en la funcion processForm de action.class.php.
Despues de $form->save();
if($form->getValue('distributor_image'))
{
// Restablesco el nombre del archivo y renombro el archivo
$Distributor = DistributorPeer::retrieveByPK($Distributor->getIdDistributor());
$fileName = explode(".",$Distributor->getDistributorImage());
$ext = strtolower($fileName[1]);
if($ext == "jpeg")
{
$ext="jpg";
}
rename(sfConfig::get('app_directory_distributor').
$Distributor->getDistributorImage() ,
sfConfig::get('app_directory_distributor')."distributor_".
$Distributor->getIdDistributor().".".$ext);
$Distributor->setDistributorImage('distributor_'.
$Distributor->getIdDistributor().'.jpg');
$Distributor->save();
}
Espero les sea de gran ayuda,
Henry Vallenilla
Symfony Developer
Skype: hvallenilla
Email/Gtalk: henryvallenilla@gmail.com
Caracas - Venezuela
www.matatigre.com
Categories: Symfony



















gracias hombre, me fue de enorme ayuda
Exito amigo !!!