Reputation: 293
I have an upload form, it works well, the photo is being uploaded but the problem is that the sfThumbnail plugin doesn't seem to work. No thumbnail is being generated. Here's my code:
// /lib/form/UploadForm.class.php
public function configure()
{
$this->setWidget('photo', new sfWidgetFormInputFileEditable(
array(
'edit_mode' => !$this->isNew(),
'with_delete' => false,
'file_src' => '',
)
));
$this->widgetSchema->setNameFormat('image[%s]');
$this->setValidator('photo', new sfValidatorFile(
array(
'max_size' => 5000000,
'mime_types' => 'web_images',
'path' => '/images/',
'required' => true,
'validated_file_class' => 'sfMyValidatedFileCustom'
)
));
And here's for the validator class
class sfMyValidatedFileCustom extends sfValidatedFile{
public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777)
{
$saved = parent::save($file, $fileMode, $create, $dirMode);
$thumbnail = new sfThumbnail(150, 150, true, true, 75, '');
$location = strpos($this->savedName,'/image/');
$filename = substr($this->savedName, $location+15);
// Manually point to the file then load it to the sfThumbnail plugin
$uploadDir = sfConfig::get('sf_root_dir').'/image/';
$thumbnail->loadFile($uploadDir.$filename);
$thumbnail->save($uploadDir.'thumb/'.$filename,'image/jpeg');
return $saved;
}
And my actions code:
public function executeUpload(sfWebRequest $request)
{
$this->form = new UploadForm();
if ($request->isMethod('post'))
{
$this->form->bind(
$request->getParameter($this->form->getName()),
$request->getFiles($this->form->getName())
);
if ($this->form->isValid())
{
$this->form->save();
return $this->redirect('photo/success');
}
}
}
I'm not 100% sure if I am doing it correctly but this is what I have seen from the docs and other examples.
Upvotes: 3
Views: 480
Reputation: 22756
You can't use $this->savedName
because it's a protected value from sfValidatedFile
. You should use $this->getSavedName()
instead.
I don't understand this part:
$location = strpos($this->savedName,'/image/');
$filename = substr($this->savedName, $location+15);
Why do you extract the filename, when, finally, you re-add /image/
to it when load it with loadFile
?
Any way, I made some change on your class. I didn't tested it but I think it should work.
class sfMyValidatedFileCustom extends sfValidatedFile
{
public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777)
{
$saved = parent::save($file, $fileMode, $create, $dirMode);
$filename = str_replace($this->getPath().DIRECTORY_SEPARATOR, '', $saved);
// Manually point to the file then load it to the sfThumbnail plugin
$uploadDir = $this->getPath().DIRECTORY_SEPARATOR;
$thumbnail = new sfThumbnail(150, 150, true, true, 75, '');
$thumbnail->loadFile($uploadDir.$saved);
$thumbnail->save($uploadDir.'thumb/'.$filename, 'image/jpeg');
return $saved;
}
Upvotes: 3