middlewares/image-manipulation

按需处理图像的中间件

v2.0.1 2020-12-04 00:00 UTC

This package is auto-updated.

Last update: 2024-08-29 03:47:40 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

按需转换图像的中间件,允许调整大小、裁剪、旋转和转换为其他格式。使用imagecow库,该库可以检测和使用GdImagick,并提供对客户端提示和不同的自动裁剪方法的支持。

使用lcobucci/jwt对图像路径和处理选项进行编码,以生成uri,从而防止修改和图像调整大小攻击。

注意: 为了保持单一职责原则(SRP),此中间件不提供以下功能,这些功能应委托给其他中间件

  • 从目录中读取图像:此库仅处理内部中间件返回的图像响应,不读取文件系统。

  • 图像缓存:库返回一个包含处理后的图像的响应,但不提供任何缓存系统。

可以与middlewares/filesystem结合使用,允许读写文件系统。(见下面的示例)。

要求

  • PHP >= 7.2
  • PSR-7 http库
  • PSR-15中间件分发器

安装

此软件包可通过Composer安装和自动加载,名称为middlewares/image-manipulation

composer require middlewares/image-manipulation

示例

以下示例还使用了middlewares/filesystem来读取/保存处理后的图像。

use Middlewares\ImageManipulation;
use Middlewares\Reader;
use Middlewares\Writer;

//You need a signature key
$key = 'sdf6&-$<@#asf';

//Manipulated images directory
$cachePath = '/path/to/cache';

//Original images directory
$imagePath = '/path/to/images';

$dispatcher = new Dispatcher([
    //read and returns the manipulated image if it's currently cached
    Reader::createFromDirectory($cachePath)->continueOnError(),

    //saves the manipulated images returned by the next middleware
    Writer::createFromDirectory($cachePath),

    //transform the image
    new Middlewares\ImageManipulation($key),

    //read and return a response with original image if exists
    Reader::createFromDirectory($imagePath)->continueOnError(),

    //In your views
    function () {
        //Create a manipulated image uri
        $uri = Middlewares\ImageManipulation::getUri('image.jpg', 'resizeCrop,500,500,CROP_ENTROPY');

        echo "<img src='{$uri}' alt='Manipulated image' width=500 height=500>";
    }
]);

$response = $dispatcher->dispatch(new ServerRequest($uri));

使用方法

您需要一个密钥来签名uri。这可以防止攻击和路径的修改。

$key = 'super-secret-key';

$imageManipulation = new Middlewares\ImageManipulation($key);

可选地,您可以提供Psr\Http\Message\StreamFactoryInterface作为第二个参数,以创建带有图像的新响应流。如果没有定义,将使用Middleware\Utils\Factory自动检测。

$key = 'super-secret-key';
$streamFactory = new MyOwnStreamFactory();

$imageManipulation = new Middlewares\ImageManipulation($key, $streamFactory);

clientHints

此选项允许使用客户端提示,默认情况下是禁用的。如果以默认参数调用此方法,允许的提示是['Dpr', 'Viewport-Width', 'Width']。请注意,客户端提示仅由Chrome和Opera浏览器支持

library

要使用的库。可以是GdImagick。如果没有指定,将自动检测。

辅助工具

getUri

为了简化uri的创建,提供了这个接受三个参数的静态方法

  • $image:图像路径。此值用于替换请求的uri路径,以便传递给下一个中间件。
  • $transform:转换详情。您可以使用任何imagecow api 方法作为字符串,例如
    • resize,200:将图片宽度调整为200像素(高度自动调整)
    • crop,200,500:将图片裁剪为200x500像素(居中裁剪)
    • crop,100,100,CROP_ENTROPY:使用熵方法裁剪图片为100x100像素,以找到图片中最有趣的部分
    • resize,300|rotate,90|format,jpg:将图片宽度调整为300像素,旋转90度并转换为jpg格式
  • $signatureKey:可选的签名密钥,用于签名uri路径。如果未提供,则使用传递给中间件的相同密钥。
use Middlewares\ImageManipulation;

$image = '/img/avatar.jpg';
$transform = 'resizeCrop,200,200';

$uri = ImageManipulation::getUri($image, $transform);

echo '<img src="'.$uri.'" alt="My image">';

有关最近更改的更多信息,请参阅变更日志,有关贡献详情,请参阅贡献指南

MIT许可证(MIT)。有关更多信息,请参阅许可证