图像调整大小zf2 [英] image resize zf2

查看:54
本文介绍了图像调整大小zf2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在zend Framework 2中实现图像调整大小功能(最好具有gd2库扩展名).

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 references?

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

If i want to create one, where should I add it. In older Zend framework, there was a concept of Action Helper, what about Zend framework 2 ?

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

Please suggest the best solution here.

推荐答案

我目前同时使用想象使用 Zend Framework 2 进行处理.

I currently use Imagine together with Zend Framework 2 to handle this.

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

  1. Install Imagine: php composer.phar require imagine/Imagine:0.3.*
  2. Create a service factory 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 (hereby 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 the "quick and dirty" way, since you should do following (optional but good practice for re-usability):

    1. 可能在服务中处理图像转换
    2. 从服务中检索图像
    3. 使用输入过滤器来验证文件和参数
    4. 缓存输出(请参见最终http://zend-framework-community.634137.n4.nabble.com/How-to-handle-404-with-action-controller-td4659101.html )
    1. probably handle image transformations in a service
    2. retrieve images from a service
    3. use an input filter to validate files and parameters
    4. cache output (see http://zend-framework-community.634137.n4.nabble.com/How-to-handle-404-with-action-controller-td4659101.html eventually)

    相关: Zend Framework-使用控制器返回图像/文件

    这篇关于图像调整大小zf2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆