raffaelj/cockpit-imageresize 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

raffaelj/cockpit-imageresize

Composer 安装命令:

composer require raffaelj/cockpit-imageresize

包简介

Resize and optimize uploaded images automatically in Cockpit CMS

README 文档

README

This addon is not compatible with Cockpit CMS v2.

See also Cockpit CMS v1 docs, Cockpit CMS v1 repo and Cockpit CMS v2 docs, Cockpit CMS v2 repo.

Addon for Cockpit CMS to resize and optimize uploaded images automatically

Requirements, compatibility

Since v0.2.0, ImageResize requires Cockpit v0.10.1 or above.

I rewrote a lot of the code to make use of the new cockpit.asset.upload event. It should now also be compatible with the cloud storage addon, but I wasn't able to test it, yet.

If you enable the optimize option, the [ImageOptimizer addon] is obsolute. Don't use both addons together.

Installation

Copy this repository into /addons and name it ImageResize or use the cli.

via git

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

via cp cli

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

via composer

Make sure, that the path to cockpit addons is defined in your projects' composer.json file.

{
    "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

Usage

A copy of the original file is stored in /uploads/full/filename.jpg. The default file will be replaced with the resized file. From now on, you don't have to create thumbnails from 8MB sized files again, but you are still able to use the original file, if you want to.

If users upload very large images, it will take a while to process the files.

There is no option in the assets manager to choose the original file, but your api autput has two additional keys:

original keys:

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

extra keys:

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

Options

The GUI is outdated and doesn't provide all options.

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

If users without admin rights should have access to the settings, you have to give them manage rights.

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

CLI

Call ./cp imageresize/replace to replace all existing images.

Warning

  • Create a backup before processing.
  • Make sure to set memory_limit = 512M in your php.ini if you have to process large files.
  • Use a sequential bash command if you still have memory issues

If you run into memory problems, use the script sequentially. First call the following command and read the output, e. g. "151".

./cp imageresize/replace --count

Then call the following command and replace "151" with your assets count. Now the script processes only 10 files at once.

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

Customized assetsmanager/cp-assets component

extra features:

  • "select all" checkbox
  • copyright field for assets
  • select different sizes (profiles) in assetsmanager

Spatie image optimizer

If you wonder, why the optimizer doesn't work, you have to install some binaries in your environment. See: https://github.com/spatie/image-optimizer#optimization-tools

I tested it successfully on my local devolopment machine with the raffaelj/php7-apache-imgopt docker image.

To do

  • batch action for existing files
  • cli commands
    • batch convert all assets
    • update entries
  • multiple profiles, e. g. "thumbnail", "banner"...
  • overwrite default date pattern in uploads folder to custom folder
  • force recreation when changing defaults
  • GUI for profiles
  • fine tuning for image optimizer (especially SVG)

Credits and third party libraries

raffaelj/cockpit-imageresize 适用场景与选型建议

raffaelj/cockpit-imageresize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「image」 「resize」 「optimize」 「cockpit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 raffaelj/cockpit-imageresize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 raffaelj/cockpit-imageresize 我们能提供哪些服务?
定制开发 / 二次开发

基于 raffaelj/cockpit-imageresize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 36
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-15