PHP-Symfony 1.4 sfThumbnail不生成缩略图
作者:互联网
我有一个上传表单,效果很好,正在上传照片,但问题是sfThumbnail插件似乎无法正常工作.没有生成缩略图.这是我的代码:
// /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'
)
));
这是验证器类的
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;
}
而我的动作代码:
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');
}
}
}
我不确定100%是否正确执行了操作,但这是我从文档和其他示例中看到的内容.
解决方法:
您不能使用$this-> savedName,因为它是sfValidatedFile中的受保护值.您应该改用$this-> getSavedName().
我不明白这部分内容:
$location = strpos($this->savedName,'/image/');
$filename = substr($this->savedName, $location+15);
为什么要提取文件名,最后在用loadFile加载时将/ image /重新添加到文件名中?
无论如何,我对您的课进行了一些更改.我没有测试过,但我认为它应该可以工作.
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;
}
标签:symfony-1-4,file-upload,symfony-forms,thumbnails,php 来源: https://codeday.me/bug/20191123/2066388.html