Zend framework2 图像大小zf2

Zend framework2 图像大小zf2,zend-framework2,image-resizing,Zend Framework2,Image Resizing,我需要在zend framework 2中实现图像大小调整功能(最好使用gd2库扩展) 我找不到相同的组件/帮助程序。有推荐人吗 如果我想创建一个,我应该在哪里添加它。在旧的Zend框架中,有一个动作助手的概念,那么Zend框架2呢 请在此建议最佳解决方案。为此使用服务,并将其注入需要该功能的控制器。我目前与一起使用来处理此问题 安装Imagine:php composer.phar需要Imagine/Imagine:0.3. 为Imagine服务创建服务工厂(在YourModule::getS

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

我找不到相同的组件/帮助程序。有推荐人吗

如果我想创建一个,我应该在哪里添加它。在旧的Zend框架中,有一个动作助手的概念,那么Zend框架2呢


请在此建议最佳解决方案。

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

我目前与一起使用来处理此问题

  • 安装Imagine:
    php composer.phar需要Imagine/Imagine:0.3.
  • Imagine
    服务创建服务工厂(在
    YourModule::getServiceConfig
    中):

  • 在逻辑中使用它(这里是一个控制器的小示例):

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

  • 可能在服务中处理图像转换
  • 从服务中检索图像
  • 使用输入筛选器验证文件和参数
  • 缓存输出(请参阅)

  • 相关:

    要动态调整上传图像的大小,您应该执行以下操作:

    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;
    }
    
    下面是Zend Framework 2中的一个模块。看看这个。它有一些很好的特性,比如-

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

    对于那些无法整合
    的人,想象一下像我这样的人

    我找到了另一个对我来说非常有效的解决方案。如果您不想阅读完整的文档,这里没有什么解释:

    运行:
    php composer.phar需要webino/webino图像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'
        ),
    

    祝你好运

    将映像服务(更容易地为测试注入mock等)注入控制器,而不是在其中使用服务定位器,这不是更好吗?的确如此。这无疑是一个快速而肮脏的版本。我是国际奥委会强有力的支持者(见)@Ocramius非常有用的答案。谢谢,我会检查它的执行情况。
    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;
    }
    
    <?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()));
      }