2014-06-23 2 views
-1

저는 Zend Frame을 처음 사용하기 때문에 zend framework 2에서 업로드하는 동안 이미지 크기를 조정해야합니다. image resize zf2에서이 방법을 사용하려고 시도했지만 나에게 적합하지 않았습니다.ZF2에서 업로드하는 동안 이미지의 크기를 조정하는 방법

도와주세요.

공공 기능 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

+0

당신이 좀 더 구체적인 수 있을까? –

+0

이미지를 업로드 할 수는 있지만 크기를 조정할 수는 없습니다. – udith

+0

도움이 될 수 있습니다. http://stackoverflow.com/questions/14832891/image-resize-zf2 – cptnk

답변

0

나는 module.It 나를 위해 쉬운 방법입니다 젠드하는 외부 라이브러리를 추가하여이 질문에 대한 답변을 얻을. 외부 라이브러리로 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/

+0

제공된 링크가 전혀 작동하지 않습니다 .. !! – Ritesh

관련 문제