wa72/adaptimage

调整自适应图像到预定义大小,生成缩略图,并缓存

v0.5.2 2024-04-26 14:52 UTC

This package is auto-updated.

Last update: 2024-08-26 15:41:51 UTC


README

Build Status Latest Version

A small PHP library that lets you easily resize images to pre-defined sizes (useful for adaptive images), generate thumbnails, and cache them.

Built on top of the Imagine library, it is designed to be framework agnostic, object orientated, customizable, and extendable.

特性:

  • 允许定义多个允许的图像大小。每个定义的图像大小都可以给予额外的Imagine过滤器,例如用于锐化。
  • 调整大小的图像被写入缓存文件,文件名是根据输入图像和应用的转换计算得出的。你可以通过实现OutputPathGeneratorInterface来轻松编写自己的命名规则。下一次需要调整大小的图像时,将返回缓存文件。当原始文件更改时,缓存文件将被重新生成。
  • 新增:根据HTML5规范提供响应式图像的辅助类,具有srcsetsizes属性,这些属性生成调整大小的图像文件和srcset属性的值。
  • 自适应图像:为任意屏幕宽度获取图像,并获取最接近的已定义大小的调整大小图像。您可以选择是否要下一个更小的图像(完全适合给定宽度)或下一个更大的图像(浏览器可以将其缩小以填充屏幕)。
  • 缩略图:支持“内嵌”和“外延”(裁剪)模式,支持单个图像的自定义裁剪。
  • 可以模拟调整大小操作,即计算图像的结果宽度和高度,而不实际转换它。这对于在HTML页面中生成具有宽度和高度属性的许多缩略图<img>标签非常有用。
  • 在生成调整大小图像时使用锁文件以防止竞态条件。

安装

composer require "wa72/adaptimage"

使用方法

首先,在应用程序中定义允许的图像大小,匹配您的CSS媒体查询断点。这是通过使用ImageResizeDefinition对象完成的,除了所需的图像宽度和高度外,还可以包含额外的转换过滤器,例如用于锐化或添加水印。

use Wa72\AdaptImage\ImageResizeDefinition;
use Wa72\AdaptImage\ImagineFilter\Sharpen;

$sizes = array(
    ImageResizeDefinition::create(1600, 1200),
    ImageResizeDefinition::create(1280, 1024),
    ImageResizeDefinition::create(768, 576),
    ImageResizeDefinition::create(1024, 768)->addFilter(new Sharpen() // example with additional sharpen filter
);

接下来,我们需要一个实现OutputPathGeneratorInterface的对象,该对象能够计算调整大小图像应存储的路径和文件名(取决于输入文件和应用的转换)。OutputPathGeneratorBasedir是一个类,它生成输出文件名,这些文件名都位于公共基本目录中的每个转换子目录中。

use Wa72\AdaptImage\Output\OutputPathGeneratorBasedir;

$cachedir = __DIR__  . '/cache';
$output_path_generator = new OutputPathGeneratorBasedir($cachedir);

现在我们可以定义一个AdaptiveImageResizer和一个ThumbnailGenerator,它们将为我们完成工作

use Wa72\AdaptImage\AdaptiveImageResizer;
use Wa72\AdaptImage\ThumbnailGenerator;
use Imagine\Imagick\Imagine; // or some other Imagine version

$imagine = new Imagine();

$thumbnail_generator = new ThumbnailGenerator($imagine, $output_path_generator, 150, 150, 'inset');
$resizer = new AdaptiveImageResizer($imagine, $output_path_generator, $sizes);

两者ThumbnailGeneratorAdaptiveImageResizer都接收一个代表原始文件的ImageFileInfo对象作为输入,并将返回另一个指向生成的新尺寸图像文件的ImageFileInfo对象。ThumbnailGenerator将根据构造函数中定义的尺寸生成缩略图,而AdaptiveImageResizer将从定义的图像尺寸中选择最适合$client_screen_width的尺寸,并将图像缩放到该尺寸。只有在还没有缩放版本或者原始文件比缩放版本新时,两者才会缩放图像,如果已存在,则仅返回缓存的文件。

use Wa72\AdaptImage\ImageFileInfo;

$image = ImageFileInfo::createFromFile('/path/to/original/image');

$thumbnail = $thumbnail_generator->thumbnail(true, $image);

$client_screen_width = 1024; // typically you get this value from a cookie

$resized_image = $resizer->resize(true, $image, $client_screen_width);

$thumbnail$resized_image是包含生成文件路径、名称、图像类型、宽度和高度的ImageFileInfo对象。使用这些信息生成HTML的img标签,或将缩放图像提供给用户,例如使用Symfony中的BinaryFileResponse

$response = new Symfony\Component\HttpFoundation\BinaryFileResponse($resized_image->getPathname());
$response->prepare($request);
return $response;

有关更多文档,请参阅源代码,它应该有很好的注释。

© 2015 Christoph Singer. 在MIT许可证下授权。