nelson/resizer

此包的最新版本(8.2.0)没有可用的许可证信息。

Nette 框架与 Latte 的 Resizer 扩展

8.2.0 2023-06-26 12:09 UTC

README

  1. Composer: composer require nelson/resizer

  2. 注册

    extensions:
    	resizer: Nelson\Resizer\DI\ResizerExtension
  3. 配置

    这是所需的最小配置

    resizer:
    	library: 'Imagick' # Imagick|Gmagick|Gd
    	wwwDir: %wwwDir%
    	tempDir: %tempDir%

    其他选项及其默认值

    resizer:
    	interlace: true # for progressive JPEGs
    	strip: true # removes image metadata, color profiles etc.
    	cache: '/resizer/'
    	qualityAvif: 70 # 0 - 100
    	qualityWebp: 70 # 0 - 100
    	qualityJpeg: 70 # 0 - 100
    	compressionPng: 9 # 0 - 9
    	upgradeJpg2Webp: true # automatically convert JPEGs to WEBP when the format is supported by the browser & server
    	upgradeJpg2Avif: true # automatically convert JPEGs to AVIF when the format is supported by the browser & server
    	upgradePng2Webp: true # automatically convert PNGs to WEBP when the format is supported by the browser & server
    	upgradePng2Avif: true # automatically convert PNGs to AVIF when the format is supported by the browser & server

还提供了 AVIF & WEBP 的升级/降级逻辑。首选的是原始的 AVIF/WEBP,然后是其他格式,最后是 JPEG。

如果同时启用了升级,则优先使用 AVIF 而不是 WEBP。

用法

参数顺序取决于路由器,默认为

  1. 尺寸和修饰符。 string
  2. 图像文件。 string
  3. 格式。 string

尺寸和修饰符

语法:- [ifresize-][[cropX]width]x[[cropY]height][forceDimensions][-quality]

尺寸

  • 100x100 - 宽度和高度必须相等或更小,按 AR 缩放。
  • x100 - 高度必须相等或更小。
  • 100x - 宽度必须相等或更小。
  • 100x50! - 强制尺寸而不尊重 ifresize 和 AR。
  • 裁剪
    • 宽度: l - 左,c - 中,r - 右。
    • 高度: t - 上,c - 中,b - 下。
    • 示例: c100xc100

修饰符

  • ifresize
    • 示例: ifresize-100x200
    • 如果图像的宽度或高度小于期望值,则不会放大图像。
  • 质量
    • 示例: x-q50 - 仅设置质量而不进行任何缩放。
    • 对于 WEBP/AVIF,质量设置为 100 表示无损压缩。

格式

  • 可以使用格式参数在图像文件格式之间切换,例如在 <picture> 标签中使用 <source srcset=""> 将 jpeg 转换为 WEBP/AVIF。

相对和绝对 URL

相对

  • <img src="{rlink 'test.jpg', '200x100'}">

绝对,使用不同的标签

  • <img src="{rlinkabs 'test.jpg', 'l400xc200'}">
  • <a href="{rlinkabs 'test.jpg', 'l400xc200'}" target="_blank">图片链接</a>