phpfile-uploadzend-framework2image-resizing

How to resize a image while uploading in ZF2


I'm new to Zend Frame work and I need to implement image resize while uploading in zend framework 2. I try to use the method in image resize zf2 but it didnot work for me.

please help?

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);
}

Related to :-image resize zf2


Solution

  • I get answer for this question by adding external library to zend module.It is a easy way for me. i used http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ class as external library.this is my controller class.

    class ProfileController extends 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);
    }
    

    }

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