raffaelj/cockpit-imageresize

在 Cockpit CMS 中自动调整上传图片的大小并优化

安装: 36

依赖: 1

建议: 0

安全: 0

星级: 8

观察者: 4

分支: 3

开放问题: 4

类型:cockpit-module

0.2.3 2022-11-17 11:07 UTC

This package is auto-updated.

Last update: 2024-09-11 19:01:43 UTC


README

此插件与 Cockpit CMS v2 不兼容。

请参阅Cockpit CMS v1 文档Cockpit CMS v1 仓库以及Cockpit CMS v2 文档Cockpit CMS v2 仓库

Cockpit CMS 的插件,用于自动调整上传图片的大小和优化

需求、兼容性

自 v0.2.0 版本起,ImageResize 需要 Cockpit v0.10.1 或更高版本。

我重写了大量代码以利用新的 cockpit.asset.upload 事件。现在它也应该与云存储插件兼容,但我还没有进行测试。

如果您启用了 optimize 选项,则 [ImageOptimizer 插件] 已过时。不要同时使用这两个插件。

安装

将此存储库复制到 /addons 目录,并命名为 ImageResize,或者使用 cli。

通过 git

cd path/to/cockpit
git clone https://github.com/raffaelj/cockpit_ImageResize.git addons/ImageResize

通过 cp cli

cd path/to/cockpit
./cp install/addon --name ImageResize --url https://github.com/raffaelj/cockpit_ImageResize/archive/master.zip

通过 composer

请确保在项目的 composer.json 文件中已定义 cockpit 插件路径。

{
    "name": "my/cockpit-project",
    "extra": {
        "installer-paths": {
            "addons/{$name}": ["type:cockpit-module"]
        }
    }
}
cd path/to/cockpit-root
composer create-project --ignore-platform-reqs aheinze/cockpit .
composer config extra.installer-paths.addons/{\$name} "type:cockpit-module"

composer require --ignore-platform-reqs raffaelj/cockpit-imageresize

用法

原始文件副本存储在 /uploads/full/filename.jpg。默认文件将被调整大小的文件替换。从此刻起,您不需要再从 8MB 大小的文件中创建缩略图,但如果需要,您仍然可以使用原始文件。

如果用户上传非常大的图片,处理文件将需要一段时间。

在资产管理器中没有选项可以选择原始文件,但您的 API 输出有两个额外的键

原始键

"path": "/2019/07/02/filename.jpg",
"title": "DSC07504.JPG",
...

额外键

"sizes": {
    "full": {
        "path": "/full/filename.jpg",
        "width": 4912,
        "height": 3264,
        "size": 4390912
    }
},
"resized": true,

选项

GUI 已过时,不提供所有选项。

config/config.php:

<?php
return [
    'app.name' => 'ImageResize Test',

    'imageresize' => [
        'resize'       => false,        # (bool) default: true

        # create a copy of uploaded files in `/original/img.jpg`
        'keepOriginal' => true,         # (bool) default: true
        'moveOriginalTo' => 'original', # (string) default: full

        # resize options, that are passed to SimpleImage library
        # If you set maxWidth or maxHeight to 0 (zero), the value will be ignored.
        'maxWidth'     => 1920,         # (int) default: 1920
        'maxHeight'    => 0,            # (int) default: 0
        'method'       => 'bestFit',    # (string) default: bestFit
        'quality'      => 100,          # (int) default: 100

        # remove uniqid from file names, duplicates will have increasing number suffixes
        'prettyNames'  => true,         # (bool) default: false

        # overwrite original date pattern - `/2020/10/30/img.jpg` --> `/images/img.jpg`
        'customFolder' => '/images',    # (string|null) default: null

        # Spatie image optimizer (requires additional binaries)
        'optimize'     => true,         # (bool) default: false

        # use modified assets manager
        'replaceAssetsManager' => true, # (bool) default: false

        # add multiple sizes like thumbnail
        'profiles' => [
            'small' => [                # --> `/small/img.jpg`
                'width'   => 500,
                'height'  => 0,
                'method'  => 'bestFit', # (string) default: thumbnail
                'quality' => 70,        # (int) default: 100
            ],
            'thumbs' => [               # --> `/thumbs/img.jpg`
                'width'   => 100,
                'height'  => 100,
                'method'  => 'thumbnail',
                'quality' => 70,
            ],
            'headerimage' => [
                'width'   => 1200,
                'height'  => 400,
                'method'  => 'thumbnail',
                'quality' => 70,
                # set custom folder, that doesn't match profile name --> `/header/img.jpg`
                'folder'  => 'header'   # (string) if omited, the key name 'headerimage' is used
            ],
        ],
    ],
];

ACL

如果无管理员权限的用户应该有权访问设置,您必须授予他们管理权限。

groups:
    managers:
        cockpit:
            backend: true
        imageresize:
            manage: true

CLI

调用 ./cp imageresize/replace 以替换所有现有图片。

警告

  • 在处理之前创建备份。
  • 如果需要处理大文件,请确保在您的 php.ini 中设置 memory_limit = 512M
  • 如果您仍然有内存问题,请使用顺序的 bash 命令

如果您遇到内存问题,请按顺序使用脚本。首先调用以下命令并读取输出,例如“151”。

./cp imageresize/replace --count

然后调用以下命令,将“151”替换为您的资产计数。现在脚本一次处理 10 个文件。

for i in seq 0 10 151; do ./mp imageresize/replace --skip $i --limit 10 --s --loud; done

自定义 assetsmanager/cp-assets 组件

额外功能

  • “全选”复选框
  • 为资产添加 版权 字段
  • 在资产管理器中选择不同的尺寸(配置文件)

Spatie 图像优化器

如果您想知道为什么优化器不起作用,您必须在环境中安装一些二进制文件。请参阅:https://github.com/spatie/image-optimizer#optimization-tools

我在本地开发机器上使用raffaelj/php7-apache-imgopt docker 镜像成功测试了它。

待办事项

  • 为现有文件添加批量操作
  • CLI 命令
    • 批量转换所有资产
    • 更新条目
  • 多个配置文件,例如“缩略图”、“横幅”...
  • 在上传文件夹中覆盖默认日期格式到自定义文件夹
  • 更改默认值时强制重新创建
  • 配置文件的用户界面
  • 对图像优化器进行精细调整(特别是SVG)

致谢和第三方库