buglinjo/laravel-webp

Laravel 包用于 WebP 图像格式化。

v3.0.1 2024-06-11 14:27 UTC

This package is auto-updated.

Last update: 2024-09-11 15:13:54 UTC


README

WebP (.webp) 来到 Laravel

Latest Version on Packagist Software License Total Downloads

关于

WebP 是一种现代图像格式,它为网页上的图像提供了卓越的无损和有损压缩。使用 WebP,网站管理员和开发人员可以创建更小、更丰富的图像,使网络更快。

WebP 无损图像比 PNG 小 26%。WebP 有损图像在等效 SSIM 质量指数下比相应的 JPEG 图像小 25-34%。

无损 WebP 支持透明度(也称为 alpha 通道),仅额外占用 22% 的字节数。对于可以接受有损 RGB 压缩的情况,有损 WebP 也支持透明度,通常比 PNG 小 3 倍的文件大小。

cwebp 使用 WebP 格式压缩图像。输入格式可以是 PNGJPEGTIFFWebP 或原始的 Y'CbCr 样本。

安装前

新驱动程序可用:php-gd

目前支持 2 个驱动程序

  • php-gd - 只需安装 gd - PHP 扩展
  • cwebp - 使用 Google 原生 cwebp 命令行工具

注意:如果您选择使用 cwebp 驱动程序,在安装此包之前需要安装 WebP。
更多信息可以访问这个 页面

安装

通过 Composer

$ composer require buglinjo/laravel-webp

对于 Laravel <= 5.4

更新 composer 后,将 ServiceProvider 添加到 config/app.php 文件中的 providers 数组

Buglinjo\LaravelWebp\WebpServiceProvider::class,

您可以使用 facade 以缩短代码。将以下内容添加到您的别名中

'Webp' => Buglinjo\LaravelWebp\Facades\Webp::class,

发布配置文件

您需要发布配置文件以添加 cwebp 全局路径。

php artisan vendor:publish --provider="Buglinjo\LaravelWebp\WebpServiceProvider" --tag=config

config/laravel-webp.php 配置文件中,您应该设置 cwebp 全局路径。

return [
    /*
    |--------------------------------------------------------------------------
    | Default Quality
    |--------------------------------------------------------------------------
    |
    | This is a default quality unless you provide while generation of the WebP
    |
    */
    'default_quality' => 70,

    /*
    |--------------------------------------------------------------------------
    | Default Driver
    |--------------------------------------------------------------------------
    |
    | This is a default image processing driver. Available: ['cwebp', 'php-gd']
    |
    */
    'default_driver' => 'php-gd',

    /*
    |--------------------------------------------------------------------------
    | Drivers
    |--------------------------------------------------------------------------
    |
    | Available drivers which can be selected
    |
    */
    'drivers' => [

        /*
        |--------------------------------------------------------------------------
        | Cwebp Driver
        |--------------------------------------------------------------------------
        |
        | If you choose cwebp driver it is required to specify the path to the executable.
        |
        */
        'cwebp' => [
            'path' => '/usr/local/bin/cwebp',
        ],

        /*
        |--------------------------------------------------------------------------
        | Cwebp Driver
        |--------------------------------------------------------------------------
        |
        | If you choose PHP GD driver no configuration is necessary.
        |
        */
        'php-gd' => [
            //
        ],
    ],
];

使用方法

Webp::make(<UploadedFile image>)->save(<output path>, <quality :optional>);

注意:当使用 Laravel 请求检索文件时,会创建 UploadedFile 类实例。

示例

    $webp = Webp::make($request->file('image'));

    if ($webp->save(public_path('output.webp'))) {
        // File is saved successfully
    }

其中 <quality> 是 0 - 100 的整数。0 - 最低质量,100 - 最高质量。

默认 quality 为 70

您还可以通过在 WebP::make(<UploadedFile image>)->save(<输出路径>); 之间链式调用 ->quality(<quality>) 来设置 quality

许可证

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