despark/image-purify

图像净化器 - ps/image-optimizer 分支

v0.1.8 2020-03-20 13:43 UTC

This package is auto-updated.

Last update: 2024-09-20 23:08:42 UTC


README

Build Status Latest Stable Version Total Downloads License

关于图像净化器

受到 psliwa/image-optimizer 启发的图像优化库。它允许通过自动检测文件类型并将文件路径传递给特定链来处理图像路径。

目前我们支持以下服务器端库进行优化。您必须将它们安装到您的服务器上才能使用此库。如果您想添加更多,可以使用自己的命令。

*Giflossy 是 gifsicle 的分支,因此您可以通过排除 --lossy 参数来使用主要库(见选项)

示例用法

使用合理的默认值初始化净化器

$options = [];
$purifierFactory = new ImagePurifierFactory($options, $logger);

$purifier = $purifierFactory->create();
$purifier->purify('path/to/image.png');

ImagePurifierFactory 接受两个可选参数 $options$logger

工厂选项

这是一个工厂选项的结构

[
// This is the chain config. You can define your own chain and feed it commands
'chains' => [
    JpegChain::class => [ // Chains must be real classes which contains commands to be executed
        'commands' => [
            'mozJpeg' => [
                'bin' => 'cjpeg', // Path to the executable. The lbrary tries to resolve it itself
                'arguments' => ['-optimize', '-progressive'], // What arguments to run
                'customClass' => MozJpeg::class, // If you want you can give custom class that must implements our CommandInterface
            ],
        ],
        'first_only' => false, // If this is true only the first command will be executed
    ],
    ...
],
'suppress_errors' => false // If set to true exceptions will be catched and only logs will be written,
]);

记录器

您可以将自定义记录器传递给 ImagePurifierFactory,该记录器必须实现 Psr\Log\LoggerInterface

值得了解

Pngquant 如果文件已被优化(至少这是我们在测试中遇到的情况),则将退出代码设置为 98。如果您不抑制错误,这将由您来处理。如果发生这种情况,我们将抛出带有特殊代码 98 的 CommandException。*一般来说,确保文件只优化一次是一个好主意。示例

try{
    $purifier->purify($filePath);
}catch(CommandException $e){
    if($e->getCode() === 98){
        // handle png quant already processed
    }
}

高级用法

待完成

测试和覆盖率

您可以通过执行以下命令来运行测试

composer test

我们旨在实现 100% 的测试覆盖率。