tobento/service-imager

PHP应用程序的图像处理接口。

1.0.1 2024-03-23 14:54 UTC

This package is auto-updated.

Last update: 2024-09-23 16:02:42 UTC


README

使用Intervention Image(默认处理器)作为PHP应用程序的图像处理接口。

目录

入门

运行此命令添加运行中的imager服务项目的最新版本。

composer require tobento/service-imager

要求

  • PHP 8.0 或更高版本

亮点

  • 框架无关,适用于任何项目
  • 解耦设计

文档

基本用法

创建Imager

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\ImagerInterface;

$imager = (new ImagerFactory())->createImager();

var_dump($imager instanceof ImagerInterface);
// bool(true)

查看Imager接口以了解更多。

图像处理

use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Imager\Action;

$response = $imager
    ->resource(new File('path/image.jpg'))
    ->action(new Action\Crop(width: 200, height: 200))
    ->action(new Action\Save('path/image.webp'));
    
var_dump($response instanceof ResponseInterface);
// bool(true)

查看资源部分以了解可用的资源。

查看操作部分以了解可用的操作及其响应类型。

查看响应部分以了解响应详情。

您可能正在使用相应的方法

$response = $imager
    ->file('path/image.jpg')
    ->crop(width: 200, height: 200)
    ->save('path/image.webp');

资源

Base64资源

use Tobento\Service\Imager\Resource\Base64;
use Tobento\Service\Imager\ResourceInterface;

$resource = new Base64(
    data: 'Base64 encoded image data'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->data());
// string(25) "Base64 encoded image data"

二进制资源

use Tobento\Service\Imager\Resource\Binary;
use Tobento\Service\Imager\ResourceInterface;

$resource = new Binary(
    data: 'Binary image data'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->data());
// string(17) "Binary image data"

Data Url资源

use Tobento\Service\Imager\Resource\DataUrl;
use Tobento\Service\Imager\ResourceInterface;

$resource = new DataUrl(
    data: 'Data-URL encoded image data'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->data());
// string(27) "Data-URL encoded image data"

文件资源

use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Filesystem\File as BaseFile;
use Tobento\Service\Imager\ResourceInterface;

$resource = new File(
    file: 'path/image.jpg' // string|BaseFile
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->file() instanceof BaseFile);
// bool(true)

您可以查看文件系统服务 - 文件以了解更多信息。

流资源

use Tobento\Service\Imager\Resource\Stream;
use Tobento\Service\Imager\ResourceInterface;
use Psr\Http\Message\StreamInterface;

$resource = new Stream(
    stream: $stream // StreamInterface
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->stream() instanceof StreamInterface);
// bool(true)

URL资源

use Tobento\Service\Imager\Resource\Url;
use Tobento\Service\Imager\ResourceInterface;

$resource = new Url(
    url: 'https://www.example.com/image.jpg'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->url());
// string(33) "https://www.example.com/image.jpg"

操作

背景

为透明图像应用指定的背景颜色。

use Tobento\Service\Imager\Action\Background;

$action = new Background(
    color: '#333333' // string
);

$imager->action($action);

// or using method:
$imager->background(color: '#333333');

模糊

模糊图像。

use Tobento\Service\Imager\Action\Blur;

$action = new Blur(
    blur: 20 // int, between 0 and 100.
);

$imager->action($action);

// or using method:
$imager->blur(20);

亮度

调整图像的亮度。

use Tobento\Service\Imager\Action\Brightness;

$action = new Brightness(
    brightness: 20 // int, between -100 and 100.
);

$imager->action($action);

// or using method:
$imager->brightness(20);

着色

着色图像。

use Tobento\Service\Imager\Action\Colorize;

$action = new Colorize(
    red: 20 // int, between -100 and 100.
    green: 5 // int, between -100 and 100.
    blue: 45 // int, between -100 and 100.
);

$imager->action($action);

// or using method:
$imager->colorize(red: 20, green: 5, blue: 45);

对比度

调整图像的对比度。

use Tobento\Service\Imager\Action\Contrast;

$action = new Contrast(
    contrast: 20 // int, between -100 and 100.
);

$imager->action($action);

// or using method:
$imager->contrast(20);

裁剪

使用指定参数裁剪图像。

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200, x: 10, y: 10);

$imager->action($action);

// or using method:
$imager->crop(width: 200, height: 200, x: 10, y: 10);

编码

将图像编码到指定的MIME类型。

use Tobento\Service\Imager\Action\Encode;
use Tobento\Service\Imager\Response\Encoded;

$action = new Encode(
    mimeType: 'image/webp',
    quality: 90, // null|int
);

$response = $imager->action($action);

// or using method:
$response = $imager->encode(mimeType: 'image/webp');

// return type:
var_dump($response instanceof Encoded);
// bool(true)

查看编码响应以了解更多信息。

mimeType

支持的MIME类型

  • image/jpeg 或仅 jpeg, jpg
  • image/pjpeg 或仅 pjpeg
  • image/png 或仅 png
  • image/gif 或仅 gif
  • image/webp 或仅 webp
  • image/tiff 或仅 tiff, tif,如果处理器支持它
  • image/svg+xml 或仅 svg,如果处理器支持它
  • image/psd 或仅 psd,如果处理器支持它
  • image/bmp 或仅 bmp,如果处理器支持它
  • image/x-icon 或仅 ico,如果处理器支持它
  • image/avif 或仅 avif,如果处理器支持它

quality

图像质量从 0 到 100。

适应

将图像适配到指定的宽度和高度。

use Tobento\Service\Imager\Action\Fit;

$action = new Fit(
    width: 200,
    height: 200,
    position: Fit::CENTER, // is default
    upsize: null // is default
);

$imager->action($action);

// or using method:
$imager->fit(width: 200, height: 200);

position

  • Fit::TOP_LEFT
  • Fit::TOP
  • Fit::TOP_RIGHT
  • Fit::LEFT
  • Fit::CENTER是默认值
  • Fit::RIGHT
  • Fit::BOTTOM_LEFT
  • Fit::BOTTOM
  • Fit::BOTTOM_RIGHT

示例

// with upsize limit: (image width is 200)
$imager->fit(width: 300, height: 150, upsize: 1.5); // limits to 250

$imager->fit(width: 300, height: 150, upsize: 1); // limits to 200

$imager->fit(width: 300, height: 150, upsize: 0.5); // limits to 150

翻转

水平或垂直翻转图像。

use Tobento\Service\Imager\Action\Flip;

$action = new Flip(
    flip: Flip::HORIZONTAL, // is default
);

$imager->action($action);

// or using method:
$imager->flip(flip: Flip::HORIZONTAL);

翻转

  • Flip::HORIZONTAL
  • Flip::VERTICAL

伽马

调整图像的伽玛值。

use Tobento\Service\Imager\Action\Gamma;

$action = new Gamma(
    gamma: 2.3 // float, between 0.01 and 9.99.
);

$imager->action($action);

// or using method:
$imager->gamma(2.3);

灰度

将图像转换为灰度。

use Tobento\Service\Imager\Action\Greyscale;

$action = new Greyscale();

$imager->action($action);

// or using method:
$imager->greyscale();

定向

根据EXIF数据自动调整图像方向以正确显示。

use Tobento\Service\Imager\Action\Orientate;

$action = new Orientate();

$imager->action($action);

// or using method:
$imager->orientate();

像素化

将图像像素化。

use Tobento\Service\Imager\Action\Pixelate;

$action = new Pixelate(
    pixelate: 10 // int, between 0 and 1000.
);

$imager->action($action);

// or using method:
$imager->pixelate(10);

调整大小

根据指定参数调整图像大小。

use Tobento\Service\Imager\Action\Resize;

$action = new Resize(
    width: 200, // null|int
    height: null, // null|int
    keepRatio: true, // bool (true is default)
    upsize: null, // null|float
);

$imager->action($action);

// or using method:
$imager->resize(width: 200);

示例

// resize the image to a width of 200: (will keep aspect ratio)
$imager->resize(width: 200);

// resize the image to a height of 200: (will keep aspect ratio)
$imager->resize(height: 200);

// resize image to a fixed size:
$imager->resize(width: 200, height: 100, keepRatio: false);

// resize only the width of the image:
$imager->resize(width: 200, keepRatio: false);

// resize only the height of the image:
$imager->resize(height: 200, keepRatio: false);

// with upsize limit: (image width is 200)
$imager->resize(width: 300, upsize: 1.5); // limits to 250

$imager->resize(width: 300, upsize: 1); // limits to 200

$imager->resize(width: 300, upsize: 0.5); // limits to 150

旋转

按指定角度顺时针旋转图像,并用指定的背景颜色填充剩余的空三角形。

use Tobento\Service\Imager\Action\Rotate;

$action = new Rotate(
    degrees: 20 // float, between -360 and 360.
    bgcolor: '#ffffff' // string, is default
);

$imager->action($action);

// or using method:
$imager->rotate(degrees: 20);

棕褐色

给图像添加棕褐色滤镜。

use Tobento\Service\Imager\Action\Sepia;

$action = new Sepia();

$imager->action($action);

// or using method:
$imager->sepia();

保存

将当前处理的图像状态保存到指定的文件名。

use Tobento\Service\Imager\Action\Save;
use Tobento\Service\Imager\Response\File;

$action = new Save(
    filename: 'path/image.jpg',
    
    // The prioritized mimeType for the filename to save.
    mimeType: 'image/webp', // null|string
    
    quality: 90, // null|int
    
    overwrite: Save::OVERWRITE, // is default
    
    modeDir: 0755 // is default
);

$response = $imager->action($action);

// or using method:
$response = $imager->save(filename: 'path/image.jpg');

// return type:
var_dump($response instanceof File);
// bool(true)

查看文件响应以了解详细信息。

filename

要保存处理后的图像的文件名。

mimeType

指定文件名的优先级mimeType。如果指定,文件名将更改为相应的格式。

支持的MIME类型

  • image/jpeg 或仅 jpeg, jpg
  • image/pjpeg 或仅 pjpeg
  • image/png 或仅 png
  • image/gif 或仅 gif
  • image/webp 或仅 webp
  • image/tiff 或仅 tiff, tif,如果处理器支持它
  • image/svg+xml 或仅 svg,如果处理器支持它
  • image/psd 或仅 psd,如果处理器支持它
  • image/bmp 或仅 bmp,如果处理器支持它
  • image/x-icon 或仅 ico,如果处理器支持它
  • image/avif 或仅 avif,如果处理器支持它

quality

图像质量从 0 到 100。

overwrite

use Tobento\Service\Imager\Action\Save;
use Tobento\Service\Imager\ActionException;

modeDir

如果不存在,则从文件名创建目录模式的文件夹。

锐化

锐化图像。

use Tobento\Service\Imager\Action\Sharpen;

$action = new Sharpen(
    sharpen: 20 // int, between 0 and 100.
);

$imager->action($action);

// or using method:
$imager->sharpen(20);

响应

编码响应

use Tobento\Service\Imager\Response\Encoded;
use Tobento\Service\Imager\ResponseInterface;

$response = $imager->encode(mimeType: 'image/png');

var_dump($response instanceof ResponseInterface);
// bool(true)

var_dump($response instanceof Encoded);
// bool(true)

// returns the encoded image data:
$encodedImage = $response->encoded();
$encodedImage = (string)$response;

// Returns the base 64 encoded image data:
$base64encodedImage = $response->base64();

// Returns the data url:
$base64encodedImageDataUrl = $response->dataUrl();

// Info data:
$mimeType = $response->mimeType(); // string
$extension = $response->extension(); // string (e.g. "jpg")
$imageWidth = $response->width(); // int
$imageHeight = $response->height(); // int
$imageSize = $response->size(); // null|int

文件响应

文件响应包含处理后的图像文件。

use Tobento\Service\Imager\Response\File;
use Tobento\Service\Filesystem\File as BaseFile;
use Tobento\Service\Imager\ResponseInterface;

$response = $imager->save(filename: 'path/image.jpg');

var_dump($response instanceof ResponseInterface);
// bool(true)

var_dump($response instanceof File);
// bool(true)

var_dump($response->file() instanceof BaseFile);
// bool(true)

// returns the width of the image:
var_dump($response->width());
// int(200)

// returns the height of the image:
var_dump($response->height());
// int(100)

查看响应接口以了解详细信息。

您可以查看文件系统服务 - 文件以了解更多信息。

高级用法

分组操作

为了重复使用,您可能需要创建一个要处理的动作组。

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\Action\Action;
use Tobento\Service\Imager\Action\Processable;
use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\ActionException;

class SomeActions extends Action implements Processable
{
    /**
     * Create a new instance of SomeActions.
     *
     * @param int $width
     */
    public function __construct(
        private int $width
    ) {}
    
    /**
     * Process the action.
     *
     * @param ImagerInterface $imager
     * @param int $srcWidth
     * @param int $srcHeight     
     * @return ImagerInterface|ResponseInterface
     * @throws ActionException
     */
    public function process(
        ImagerInterface $imager,
        int $srcWidth,
        int $srcHeight
    ): ImagerInterface|ResponseInterface {
        return $imager
            ->greyscale()
            ->resize(width: $this->width);
    }

    /**
     * Returns the action parameters.
     *
     * @return array
     */
    public function parameters(): array
    {
        return [
            'width' => $this->width,
        ];
    }
    
    /**
     * Returns a description of the action.
     *
     * @return string
     */
    public function description(): string
    {
        return 'Greyscaled and resized image to width :width.';
    }
}

$imager = (new ImagerFactory())->createImager();

$response = $imager
    ->file('path/image.jpg')
    ->action(new SomeActions(width: 200))
    ->save('path/image.webp');

创建新操作

如果您想创建一个新动作,您需要单独处理每个处理器。

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\InterventionImage\Processor;
use Tobento\Service\Imager\Action\Action;
use Tobento\Service\Imager\Action\Processable;
use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\ActionException;

class SomeNewAction extends Action implements Processable
{
    /**
     * Create a new instance of SomeAction.
     *
     * @param int $foo
     */
    public function __construct(
        private int $foo
    ) {}
    
    /**
     * Process the action.
     *
     * @param ImagerInterface $imager
     * @param int $srcWidth
     * @param int $srcHeight     
     * @return ImagerInterface|ResponseInterface
     * @throws ActionException
     */
    public function process(
        ImagerInterface $imager,
        int $srcWidth,
        int $srcHeight
    ): ImagerInterface|ResponseInterface {

        if ($imager->getProcessor() instanceof InterventionImage\Processor) {            
            $image = $imager->processor()->image();
            // do something with the intervention image.
            
            return $imager;
        }
        
        // handle other processor or throw ActionException
        
        return $imager;
    }

    /**
     * Returns the action parameters.
     *
     * @return array
     */
    public function parameters(): array
    {
        return ['foo' => $this->foo];
    }
    
    /**
     * Returns a description of the action.
     *
     * @return string
     */
    public function description(): string
    {
        return 'Some new action with foo :foo processed.';
    }
}

$imager = (new ImagerFactory())->createImager();

$response = $imager
    ->file('path/image.jpg')
    ->action(new SomeNewAction(foo: 200))
    ->save('path/image.webp');

操作到消息

您可以使用消息工厂渲染处理动作的描述。

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\Message;
use Tobento\Service\Imager\ActionsInterface;
use Tobento\Service\Message\MessagesFactoryInterface;

$imager = (new ImagerFactory())->createImager();

$response = $imager
    ->file('path/image.jpg')
    ->crop(width: 200, height: 200, x: 0, y: 0)
    ->save('path/image.webp');

$messagesFactory = new Message\MessagesFactory();

var_dump($messagesFactory instanceof MessagesFactoryInterface);
// bool(true)

// You may filter out actions:
$actions = $response->actions()->withoutProcessedBy();

$messages = $messagesFactory->createMessagesFromActions(
    actions: $actions // ActionsInterface
);

foreach($messages as $message) {
    var_dump((string)$message);
}

// string(52) "Cropped image to width 200, height 200, x 0 and y 0."
// string(56) "Saved image path/image.webp with quality "90"."

您可以查看消息服务以了解详细信息。

您可以查看动作接口以了解详细信息。

接口

Imager工厂接口

use Tobento\Service\Imager\ImagerFactoryInterface;
use Tobento\Service\Imager\InterventionImage\ImagerFactory;

$imagerFactory = new ImagerFactory();

var_dump($imagerFactory instanceof ImagerFactoryInterface);
// bool(true)

createImager

创建一个新的imager。

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\ImagerInterface;

$imagerFactory = new ImagerFactory();

$imager = $imagerFactory->createImager();

var_dump($imager instanceof ImagerInterface);
// bool(true)

Imager接口

use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\InterventionImage\ImagerFactory;

$imager = (new ImagerFactory())->createImager();

var_dump($imager instanceof ImagerInterface);
// bool(true)

resource

设置要执行动作的图像资源。

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$resource = new File('path/image.jpg');

var_dump($resource instanceof ResourceInterface);
// bool(true)

$imager = $imager->resource(resource: $resource);

查看资源部分以了解可用的资源。

getResource

返回图像资源或null(如果没有设置)。

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$imager->resource(resource: new File('path/image.jpg'));

$resource = $imager->getResource();

var_dump($resource instanceof ResourceInterface);
// bool(true)

getProcessor

返回处理器或null(如果尚未可用)。

use Tobento\Service\Imager\ProcessorInterface;
use Tobento\Service\Imager\Resource\File;

$imager->resource(resource: new File('path/image.jpg'));

$processor = $imager->getProcessor();

var_dump($processor instanceof ProcessorInterface);
// bool(true)

file

将图像资源设置为文件。

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$imager->file(file: 'path/image.jpg');

var_dump($imager->getResource() instanceof File);
// bool(true)

action

添加要处理的动作。

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Action\Save;

$action = new Save(filename: 'path/image.jpg');

var_dump($action instanceof ActionInterface);
// bool(true)

$imager->action(action: $action);

查看操作部分以了解可用的操作及其响应类型。

处理器工厂接口

use Tobento\Service\Imager\ProcessorFactoryInterface;
use Tobento\Service\Imager\InterventionImage\ProcessorFactory;

$processorFactory = new ProcessorFactory();

var_dump($processorFactory instanceof ProcessorFactoryInterface);
// bool(true)

createProcessor

为指定的资源创建一个新的处理器。

use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\ProcessorInterface;
use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Imager\ProcessorCreateException;

$processorFactory = new ProcessorFactory();

$processor = $processorFactory->createProcessor(
    resource: new File('path/image.jpg'), // ResourceInterface
);

var_dump($processor instanceof ProcessorInterface);
// bool(true)

// throws ProcessorCreateException if processor could not get created.

处理器接口

use Tobento\Service\Imager\ProcessorInterface;
use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\Resource\File;

$processor = (new ProcessorFactory())->createProcessor(
    resource: new File('path/image.jpg'),
);

var_dump($processor instanceof ProcessorInterface);
// bool(true)

processAction

处理指定的动作。

use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Action;
use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\ActionProcessException;

$processor = (new ProcessorFactory())->createProcessor(
    resource: new File('path/image.jpg'),
);

$imager = (new ImagerFactory())->createImager();

$processed = $processor->processAction(
    action: new Action\Crop(200, 200), // ActionInterface
    imager: $imager, // ImagerInterface
);

// $processed might be:
var_dump($processed instanceof ImagerInterface);
// bool(true)

// or depending on the action:
var_dump($processed instanceof ResponseInterface);
// bool(false)

// throws ActionProcessException if action could not get processed.

资源接口

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$resource = new File('path/image.jpg');

var_dump($resource instanceof ResourceInterface);
// bool(true)

操作接口

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

var_dump($action instanceof ActionInterface);
// bool(true)

parameters

返回动作参数。

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

var_dump($action->parameters());
// array(4) { ["width"]=> int(200) ["height"]=> int(200) ["x"]=> NULL ["y"]=> NULL } 

description

返回动作的描述。

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

var_dump($action->description());
// string(61) "Cropped image to width :width, height :height, x x: and y :y."

processedBy

处理动作的动作。

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

$action->setProcessedBy(action: 'ClassName');

var_dump($action->processedBy());
// string(9) "ClassName"

操作接口

use Tobento\Service\Imager\ActionsInterface;
use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action\Crop;

$actions = new Actions(
    new Crop(width: 200, height: 200), // ActionInterface
);

var_dump($actions instanceof ActionsInterface);
// bool(true)

add

添加动作。

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action\Crop;

$actions = new Actions();

$actions->add(new Crop(width: 200, height: 200)); // ActionInterface

empty

如果有任何动作。

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action\Crop;

$actions = new Actions();

var_dump($actions->empty());
// bool(true)

$actions = new Actions(
    new Crop(width: 200, height: 200),
);

var_dump($actions->empty());
// bool(false)

filter

返回带有过滤动作的新实例。

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
);

$actions = $actions->filter(
    fn(ActionInterface $a): bool => in_array($a::class, [Action\Crop::class])
);

only

返回仅包含指定动作的新实例。

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
    new Action\Save(filename: 'image.jpg'),
);

$actions = $actions->only(Action\Crop::class, Action\Resize::class);

except

返回除指定动作之外的新实例。

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
    new Action\Save(filename: 'image.jpg'),
);

$actions = $actions->except(Action\Save::class, Action\Resize::class);

withoutProcessedBy

返回没有由其他动作处理的新实例。

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Sepia(),
    (new Action\Greyscale())->setProcessedBy(Action\Sepia::class),
    new Action\Save(filename: 'image.jpg'),
);

$actions = $actions->withoutProcessedBy();

all

返回所有动作。

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
);

foreach($actions->all() as $action) {
    var_dump($action instanceof ActionInterface);
    // bool(true)
}

// or just
foreach($actions as $action) {}

响应接口

use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\Response\File;
use Tobento\Service\Imager\Actions;

$response = new File(
    file: 'path/image.jpg',
    actions: new Actions()
);

var_dump($response instanceof ResponseInterface);
// bool(true)

actions

返回处理过的动作。

use Tobento\Service\Imager\Response\File;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\ActionsInterface;

$response = new File(
    file: 'path/image.jpg',
    actions: new Actions()
);

var_dump($response->actions() instanceof ActionsInterface);
// bool(true)

Intervention Image

Intervention Image imager实现。

Intervention Imager工厂

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\ImagerFactoryInterface;

$imagerFactory = new ImagerFactory(
    config: ['driver' => 'gd'], // is default
);

var_dump($imagerFactory  instanceof ImagerFactoryInterface);
// bool(true)

查看Imager工厂接口以获取其可用方法。

查看Intervention Image配置以获取可用的配置参数。

Intervention 处理器工厂

use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\ProcessorFactoryInterface;

$processorFactory = new ProcessorFactory(
    config: ['driver' => 'gd'], // is default
);

var_dump($processorFactory  instanceof ProcessorFactoryInterface);
// bool(true)

查看Processor工厂接口以获取其可用方法。

查看Intervention Image配置以获取可用的配置参数。

致谢