我正在使用http://eliteinformatiker.de/2011/09/02/thumbnails-upload-and-resize-images-with-zend_form_element_file/在 Zend Framework 中调整我的图像大小。
但实际上,它不起作用receive()
。
我的代码:
我的 Zend 表格
class Application_Form_Admin_Photos_Ajout extends Zend_Form
{
public function init()
{
/**
* FILE
*/
$this->setAttrib('enctype', 'multipart/form-data');
$maxsize = 2 * 1024 * 1024; // Premier chiffre en MB
$file = new Zend_Form_Element_File('path');
$file->setLabel("Choisissez l'image");
$file->setMaxFileSize($maxsize);
$file->addValidator('Count', false, 1);
$file->addValidator('Size', false, $maxsize);
$file->addValidator('Extension', false, 'jpg,png,gif');
$file->setRequired(true);
$this->addElement($file);
/**
* LEGENDE
*/
[...]
/**
* SUBMIT
*/
[...]
}
}
我的控制器:
if ($request->isPost())
{
if ($form->isValid($request->getPost()))
{
$file = pathinfo($form->path->getFileName());
$path = Zend_Registry::get('config')->imgPath . uniqid() . time() . '.' . $file['extension'];
$form->getElement('path')->addFilter('Rename', array('target' => $path,'overwrite' => true));
$filterChain = new Zend_Filter();
// Create the original one
$filterChain->appendFilter(new Skoch_Filter_File_Resize(array(
'directory' => Zend_Registry::get('config')->imgPath,
'width' => 10000,
'height' => 10000,
'keepSmaller' => true
)));
// Create a medium image with at most 350x200 pixels
$filterChain->appendFilter(new Skoch_Filter_File_Resize(array(
'directory' => Zend_Registry::get('config')->thumbPath,
'width' => 350,
'height' => 200,
'keepRatio' => true,
)));
$form->getElement('path')->addFilter($filterChain);
if (!$form->path->receive())
$this->_redirect('/admin/menu/menu');
}
}
有什么解决办法吗?
谢谢你。
我发现。
在 Zend Form 中替换它:
$file->addValidator('Count', false, 1);
经过,
$file->addValidator('Count', false, 2);
其中2
意味着您的 Zend Form 最多接受 2 个文件。( http://framework.zend.com/manual/1.12/en/zend.file.transfer.validators.html#zend.file.transfer.validators.count )
另一种解决方案,只需删除线,
$file->addValidator('Count', false, 1);
而这个,它不是很有用:
$filterChain->appendFilter(new Skoch_Filter_File_Resize(array(
'directory' => Zend_Registry::get('config')->imgPath,
'width' => 10000,
'height' => 10000,
'keepSmaller' => true
)));