file upload - How to resize a image while uploading in ZF2 -
i'm new zend frame work , need implement image resize while uploading in zend framework 2. seek utilize method in image resize zf2 didnot work 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)); homecoming $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 $key=>$row) { $error[] = $row; } $form->setmessages(array('fileupload'=>$error )); } else { $adapter->setdestination('./data/tmpuploads/'); if ($adapter->receive($file['name'])) { //identify uploaded errors $profile->exchangearray($form->getdata()); echo 'profile name '.$profile->profilename.' upload '.$profile->fileupload; } } } } homecoming array('form' => $form); } related :-image resize zf2
i reply question adding external library zend module.it easy way me. used http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ class external library.this 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 info post , 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 $key=>$row) { $error[] = $row; } $form->setmessages(array('fileupload'=>$error )); } else { $adapter->setdestination('//destination upload 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 uploaded file'); $image->resizetoheight(500); $image->save('//destination resized file uploaded'); } } } } homecoming array('form' => $form); } }
related:-zend framework 2 - how utilize external library http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
file-upload zend-framework2 image-resizing
No comments:
Post a Comment