image resize zf2

前端 未结 5 1459
情话喂你
情话喂你 2020-12-30 13:52

I need to implement image resize functionality (preferably with gd2 library extension) in zend framework 2.

I could not find any component/helper for the same. Any

5条回答
  •  既然无缘
    2020-12-30 14:24

    For those who are unable to integrate Imagine properly like me..

    I found another solution WebinoImageThumb here which worked perfectly fine with me. Here is little explanation if you don't want to read full documentation :

    Run: php composer.phar require webino/webino-image-thumb:dev-develop and add WebinoImageThumb as active module in config/application.config.php which further looks like :

     array(
            'Application',
            'WebinoImageThumb'
        ),
    

    .. below remains the same

    Now in your controller action use this through service locator like 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..!!

提交回复
热议问题