Resize zf2 image

I need to implement the image resizing function (preferably with the gd2 library extension) in zend framework 2.

I could not find any component / helper for this. Any links?

If I want to create it, where should I add it. The older Zend framework introduces the Action Helper concept, how about the Zend framework 2?

Please suggest the best solution here.

+6
source share
5 answers

I am currently using Imagine with Zend Framework 2 to handle this.

  • Install Imagine: php composer.phar require imagine/Imagine:0.3.*
  • Create a factory service for the Imagine service (in YourModule::getServiceConfig ):

     return array( 'invokables' => array( // defining it as invokable here, any factory will do too 'my_image_service' => 'Imagine\Gd\Imagine', ), ); 
  • Use it in your logic (here is a small example with a controller):

     public function imageAction() { $file = $this->params('file'); // @todo: apply STRICT validation! $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); $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; } 

This is obviously a β€œquick and dirty” way, since you have to do the following (optional, but good practice for reuse):

Related: Zend Framework - return image / file using the controller

+17
source

Use a service for this and add it to controllers that need functionality.

+2
source

Below is a module called WebinoImageThumb in Zend Framework 2. Check this out. It has a wonderful feature such as -

  • Resize Image
  • Cropping, scrolling, rotating, displaying and saving images
  • Create Image Reflection
+2
source

For those who cannot properly integrate Imagine like me ..

I found another WebinoImageThumb solution here that worked fine with me. Here is a little explanation if you do not want to read the full documentation:

Run: php composer.phar require webino/webino-image-thumb:dev-develop and add WebinoImageThumb as the active module in config/application.config.php , which will look like this:

 <?php return array( // This should be an array of module namespaces used in the application. 'modules' => array( 'Application', 'WebinoImageThumb' ), 

.. below remains the same

Now in the action of your controller, use this through the service locator, as shown below:

 // at top on your controller use Zend\Validator\File\Size; use Zend\Validator\File\ImageSize; use Zend\Validator\File\IsImage; use Zend\Http\Request // in action $file = $request->getFiles(); $fileAdapter = new \Zend\File\Transfer\Adapter\Http(); $imageValidator = new IsImage(); if ($imageValidator->isValid($file['file_url']['tmp_name'])) { $fileParts = explode('.', $file['file_url']['name']); $filter = new \Zend\Filter\File\Rename(array( "target" => "file/path/to/image." . $fileParts[1], "randomize" => true, )); try { $filePath = $filter->filter($file['file_url'])['tmp_name']; $thumbnailer = $this->getServiceLocator() ->get('WebinoImageThumb'); $thumb = $thumbnailer->create($filePath, $options = [], $plugins = []); $thumb->adaptiveResize(540, 340)->save($filePath); } catch (\Exception $e) { return new ViewModel(array('form' => $form, 'file_errors' => array($e->getMessage()))); } } else { return new ViewModel(array('form' => $form, 'file_errors' => $imageValidator->getMessages())); } 

Good luck .. !!

+2
source

To resize the uploaded image on the fly, you must do this:

 public function imageAction() { // ... $imagine = $this->getImagineService(); $size = new \Imagine\Image\Box(150, 150); $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; $image = $imagine->open($destinationPath); $image->thumbnail($size, $mode)->save($destinationPath); // ... } public function getImagineService() { if ($this->imagineService === null) { $this->imagineService = $this->getServiceLocator()->get('my_image_service'); } return $this->imagineService; } 
0
source

All Articles