【问题标题】:image resize zf2图像大小调整 zf2
【发布时间】:2016-01-22 00:03:08
【问题描述】:

我需要在 zend 框架 2 中实现图像大小调整功能(最好使用 gd2 库扩展)。

我找不到任何相同的组件/助手。有参考吗?

如果我想创建一个,我应该在哪里添加它。在较早的 Zend 框架中,有一个 Action Helper 的概念,那么 Zend 框架 2 呢?

请在此处提出最佳解决方案。

【问题讨论】:

    标签: zend-framework2 image-resizing


    【解决方案1】:

    我目前使用ImagineZend Framework 2 来处理这个问题。

    1. 安装想象:php composer.phar require imagine/Imagine:0.3.*
    2. Imagine 服务创建一个服务工厂(在YourModule::getServiceConfig 中):

      return array(
          'invokables' => array(
              // defining it as invokable here, any factory will do too
              'my_image_service' => 'Imagine\Gd\Imagine',
          ),
      );
      
    3. 在你的逻辑中使用它(这里是一个带有控制器的小例子):

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

    这显然是“快速而肮脏”的方式,因为您应该执行以下操作(可选但可重用性良好的做法):

    1. 可能在服务中处理图像转换
    2. 从服务中检索图像
    3. 使用输入过滤器来验证文件和参数
    4. 缓存输出(最终见http://zend-framework-community.634137.n4.nabble.com/How-to-handle-404-with-action-controller-td4659101.html

    相关:Zend Framework - Returning Image/File using Controller

    【讨论】:

    • 将图像服务(更容易注入用于测试的模拟等)注入控制器而不是使用其中的服务定位器不是更好吗?
    • 确实如此。这肯定是它的快速和肮脏的版本。我是强大的国际奥委会的坚定支持者(见ocramius.github.com/blog/…
    • @Ocramius 非常有用的答案。谢谢,将检查实施它。
    【解决方案2】:

    为此使用服务并将其注入需要该功能的控制器。

    【讨论】:

      【解决方案3】:

      这是 Zend Framework 2 中名为 WebinoImageThumb 的模块。检查一下。它有一些很棒的功能,例如 -

      • 图像调整大小
      • 图像裁剪、填充、旋转、显示和保存图像
      • 创建图像反射

      【讨论】:

        【解决方案4】:

        对于像我一样无法正确整合Imagine的人..

        我找到了另一个解决方案WebinoImageThumb here,它对我来说非常好用。如果您不想阅读完整的文档,这里几乎没有解释:

        运行:php composer.phar require webino/webino-image-thumb:dev-develop 并在config/application.config.php 中添加WebinoImageThumb 作为活动模块,进一步看起来像:

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

        .. 下面保持不变

        现在在您的控制器操作中,通过如下服务定位器使用它:

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

        祝你好运..!!

        【讨论】:

          【解决方案5】:

          为了即时调整上传图片的大小,您应该这样做:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-11-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-10
            相关资源
            最近更新 更多