如何在ZF2中上传时调整图像大小

时间:2014-06-23 11:06:56

标签: file-upload zend-framework2 image-resizing

我是Zend Frame工作的新手,我需要在zend框架2中上传时实现图像调整大小。我尝试在image resize zf2中使用该方法,但它对我不起作用。

请帮帮忙?

public function addAction(){

    $form = new ProfileForm();
    $request = $this->getRequest();  
    if ($request->isPost()) {
        $profile = new Profile();
        $form->setInputFilter($profile->getInputFilter());
        $nonFile = $request->getPost()->toArray();

        $File    = $this->params()->fromFiles('fileupload');
        $width   = $this->params('width', 30); // @todo: apply validation!
        $height  = $this->params('height', 30); // @todo: apply validation!
        $imagine = $this->getServiceLocator()->get('my_image_service');
        $image   = $imagine->open($File['tmp_name']);
        $transformation = new \Imagine\Filter\Transformation();
        $transformation->thumbnail(new \Imagine\Image\Box($width, $height));
        $transformation->apply($image);

        $response = $this->getResponse();
        $response->setContent($image->get('png'));
        $response
            ->getHeaders()
            ->addHeaderLine('Content-Transfer-Encoding', 'binary')
            ->addHeaderLine('Content-Type', 'image/png')
            ->addHeaderLine('Content-Length', mb_strlen($imageContent));
        return $response;
        $data = array_merge(
             $nonFile,
             array('fileupload'=> $File['name'])
         );
        $form->setData($data);

        if ($form->isValid()) {
            $size = new Size(array('min'=>100000)); //minimum bytes filesize
            $adapter = new \Zend\File\Transfer\Adapter\Http(); 
            $adapter->setValidators(array($size), $File['name']);
            if (!$adapter->isValid()){
                $dataError = $adapter->getMessages();
                $error = array();
                foreach($dataError as $key=>$row)
                {
                    $error[] = $row;
                }
                $form->setMessages(array('fileupload'=>$error ));
            } else {
                $adapter->setDestination('./data/tmpuploads/');

                if ($adapter->receive($File['name'])) { //identify the uploaded errors
                    $profile->exchangeArray($form->getData());
                    echo 'Profile Name '.$profile->profilename.' upload '.$profile->fileupload;
                }
            }  
        } 
    }          
    return array('form' => $form);
}

相关: - image resize zf2

1 个答案:

答案 0 :(得分:0)

我通过向zend模块添加外部库来获得这个问题的答案。对我来说这是一个简单的方法。我使用http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/类作为外部库。这是我的控制器类。

类ProfileController扩展了AbstractActionController {

public function addAction()
{
    $form = new ProfileForm();
    $request = $this->getRequest();  
    if ($request->isPost()) {

        $profile = new Profile();
        $form->setInputFilter($profile->getInputFilter());

        $nonFile = $request->getPost()->toArray();
        $File    = $this->params()->fromFiles('fileupload');

        $data = array_merge(
             $nonFile,
             array('fileupload'=> $File['name'])
         );

        //set data post and file ...    
        $form->setData($data);

        if ($form->isValid()) {

            $size = new Size(array('min'=>100000)); //minimum bytes filesize

            $adapter = new \Zend\File\Transfer\Adapter\Http(); 
            $adapter->setValidators(array($size), $File['name']);
            if (!$adapter->isValid()){
                $dataError = $adapter->getMessages();
                $error = array();
                foreach($dataError as $key=>$row)
                {
                    $error[] = $row;
                }
                $form->setMessages(array('fileupload'=>$error ));
            } else {
                $adapter->setDestination('//destination for upload the file');
                if ($adapter->receive($File['name'])) {
                    $profile->exchangeArray($form->getData());
                    //print_r($profile);
                    echo 'Profile Name '.$profile->profilename.' upload '.$profile->fileupload;

                    $image = new SimpleImage(); 
                    $image->load('//destination of the uploaded file');
                    $image->resizeToHeight(500);
                    $image->save('//destination for where the resized file to be uploaded');



                }
            }  
        } 
    }

    return array('form' => $form);
}

}

相关: - Zend Framework 2 - How to use an external library http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/