hrevert/ht-img-module

适用于 Zend Framework 2 的图像处理模块

0.6.0 2018-04-27 19:07 UTC

README

Master Branch Build Status Latest Stable Version Latest Unstable Version Total Downloads Scrutinizer Code Coverage

本模块简化了 Zend Framework 的图像处理。此模块将 zf2/zf3 与最令人惊叹的 PHP 图像处理库 Imagine 集成。

安装

  • composer require hrevert/ht-img-module
  • config/application.config.php 中注册 HtImgModule 为模块
  • 将位于 vendor/hrevert/ht-img-module/config/ht-img-module.global.php 的文件复制到 config/autoload 并根据需要进行更改

基本用法

首先,您需要在 /module/Application/config/module.config.php 中创建一个过滤器服务,例如 my_thumbnail

return [
    'htimg' => [
        'filters' => [
            'my_thumbnail' => [ // this is  filter service
                'type' => 'thumbnail', // this is a filter loader
                'options' => [  // filter loader passes these options to a Filter which manipulates the image
                    'width' => 100,
                    'height' => 100,
                    'format' => 'jpeg' // format is optional and defaults to the format of given image
                ]
            ]        
        ]
    ]
];

现在,您可以从视图模板中获取图像,例如

<img src="<?php echo $this->htImgUrl('my_image.png', 'my_thumbnail'); ?>" alt="Hello" />

或者,您也可以

  <?php echo $this->htDisplayImage('my_image.png', 'my_thumbnail', ['alt' => 'Hello']); ?>

在幕后,模块会在第一次请求时对图像应用过滤器,然后将图像缓存到网站根目录。在下一个请求中,缓存图像将直接从文件系统提供服务。

工作原理

每当您从视图模板调用过滤器服务(如 my_thumbnail),视图辅助函数(htImgUrl 和 htDisplayImage)会检查缓存图像是否存在。如果缓存图像存在,它将直接返回缓存图像的 URL。否则,它将返回显示图像的 URL。同时,在网站根目录中创建一个新的缓存图像!

文档

官方文档位于 docs/ 目录中

致谢

HtImgModule 受 AvalancheImagineBundleLiipImagineBundle 的启发。