dakzilla/intervention-image-helper

此包已被废弃,不再维护。没有建议的替代包。

Magento 2的Intervention Image Helper

1.0.6 2019-10-29 04:08 UTC

This package is not auto-updated.

Last update: 2024-02-17 17:18:21 UTC


README

一个用于在Magento 2中应用变换的实用模板助手,使用Intervention Image库。

安装

composer require dakzilla/intervention-image-helper

php bin/magento setup:upgrade

功能

  • 可以在任何前端.phtml模板中调用图像助手。无需创建自定义块!
  • 所有变换方法都可以链式调用
  • 通过以下示例中的@var DocComment,您的IDE将显示每个方法的自动完成和文档提示
  • 图像在创建时会自动缓存,并在后续每次调用时从缓存中加载
  • 可以直接在模板中设置JPEG质量
  • 与其他图像处理/优化模块兼容良好

使用方法

从模板变换图像

使用以下代码从任何前端 (.phtml) 模板调用图像助手

<?php
/** @var \Dakzilla\Intervention\Helper\Image $imageHelper */
$imageHelper = $this->getImageHelper();
?>

现在您可以通过相对图像路径或图像URL调用make方法

$image = $imageHelper->make('test/Pineapple.jpg')

Will resolve and load the file at /path/to/your/site/pub/media/test/Pineapple.jpg

然后,您可以链式调用所需的变换方法,最后调用get方法以获取到缓存图像的http链接

$imageUrl = $image->flip()
    ->invert()
    ->resize(350, null, function ($constraint) {
        $constraint->aspectRatio();
    })
    ->get();
    
# Will return http://mysite.com/media/cache/dakzilla_intervention/<cache key>/myimage.jpg

或者,您可以使用简短的链式语法一次完成所有这些操作

<img src="<?php echo $this->getImageHelper()
    ->make('test/Pineapple.jpg')
    ->flip()
    ->invert()
    ->fit(350, 700)
    ->get(); ?>">

启用JPEG压缩并设置质量

可以通过降低最终输出质量来减小JPEG的大小。PNG不会压缩,因为它们是无损格式。从模板调用图像助手的此方法以启用JPEG压缩并设置质量(1-100,从低到高)

<?php
/** @var \Dakzilla\Intervention\Helper\Image $imageHelper */
$imageHelper->make('path/to/my/image.jpeg')->setQuality(75);
?>

清除图像缓存

删除图像缓存目录。默认情况下,这是<magento root>/pub/media/cache/dakzilla_intervention为了避免潜在的权限问题,在删除后不要重新创建此文件夹。让Magento自动根据服务器权限重新创建它。

可用的变换方法

助手为每个可用的变换方法提供IDE兼容的方法提示。大部分方法都是不言自明的。有关这些方法的更多信息,请参阅Intervention Image文档

  • blur
  • brightness
  • circle
  • colorize
  • contrast
  • crop
  • ellipse
  • fill
  • flip
  • fit
  • gamma
  • greyscale
  • heighten
  • insert (例如,用于水印应用)
  • interlace
  • invert
  • limitColors
  • 遮罩
  • 不透明度
  • 方向(适用于具有EXIF数据的相机图片)
  • 像素
  • 像素化
  • 多边形
  • 矩形
  • 调整大小
  • 调整画布大小
  • 旋转
  • 锐化
  • 文本
  • 裁剪
  • 加宽

问题

  • 此模块不包含测试
  • 此模块不提供选择图像处理库的方式(默认为GD)
  • Intervention Image库的一些功能(如画布或输出流)不可用(但可能以后会实现)

兼容性

此模块已在Magento 2.1至2.3之间进行测试。由于Magento 2仍在快速发展,无法保证它将与每个版本兼容。然而,由于此包在实现上不张扬且尊重Magento 2最佳编码实践,我认为它不会在您的安装中引起问题。

要求

  • PHP 7.0及以上
  • 在Magento开源2.1 - 2.3和商业版2.2上进行了测试
  • GD库
  • Fileinfo扩展

待办事项

  • 提供从管理界面和命令行清除图像缓存的方法
  • 测试
  • 为最终用户提供更多控制,例如选择图像处理库
  • 允许缓存图像过期
  • 通过cron自动清理过期的图像缓存
  • 创建图像模板(预制的样式)以提供更短、更干净的模板语法的方法
  • 在HTTP头中添加Etags以更好地缓存失效

感谢

此简单包借鉴了Oliver Vogel的出色工作以及他的Intervention ImageImage Cache包。

此模块还受到了Stämpfli AG及其Magento 2 Image Resizer模块的启发。

许可

此模块在MIT许可下授权

版权所有2017 Simon Dakin

在蒙特利尔用♥制作