定制 joshembling/image-optimizer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

joshembling/image-optimizer

Composer 安装命令:

composer require joshembling/image-optimizer

包简介

Optimize your Filament images before they reach your database.

README 文档

README

Caution

There are no plans to extend this plugin's lifetime beyond Filament v3. Please do not plan to use this in production if you are using or upgrading to Filament v4.

Optimize your Filament images before they reach your database.

Latest Version on Packagist Total Downloads

When you currently upload an image using the native Filament component FileUpload, the original file is saved without any compression or conversion.

Additionally, if you upload an image and use conversions with SpatieMediaLibraryFileUpload, the original file is saved with its corresponding versions provided on your model.

What if you'd rather convert and reduce the image(s) before reaching your database/S3 bucket? Especially in the case where you know you'll never need to save the original image sizes the user has uploaded.

🤳 This is where Filament Image Optimizer comes in.

You use the same components as you have been doing and have access to two additional methods for maximum optimization, saving you a lot of disk space in the process. 🎉

Contents

Installation

You can install the package via composer, which currently works with the latest Filament version (^3.2) and Laravel 10, 11 & 12:

composer require joshembling/image-optimizer

If you are using Filament 3.0 or 3.1 install with:

composer require joshembling/image-optimizer:v1.2

Usage

Filament version

You must be using Filament v3.x to have access to this plugin.

For specific versions that match your PHP, Laravel, Filament and Image Optimizer installations please see the table below:

PHP Laravel version Filament version Image Optimizer version
^8.1 ^10.0 ^3.0 1.2
^8.1 ^10.0 ^3.1 1.2
^8.1 ^10.0 ^3.2 ~1.3
^8.2 ^10.0, ^11.0 ^3.2 ^1.4
^8.2 ^10.0, ^11.0, ^12.0 ^3.2 ^1.6

Server

GD Library must be installed on your server to compress images.

Optimizing images

Before uploading your image, you may choose to optimize it by converting to your chosen format. The file saved to your disk will be the converted version only.

E.g. I want to convert my image to 'webp':

use Filament\Forms\Components\FileUpload;

FileUpload::make('attachment')
    ->image()
    ->optimize('webp'),

You can do exactly the same using SpatieMediaLibraryFileUpload:

use Filament\Forms\Components\SpatieMediaLibraryFileUpload;

SpatieMediaLibraryFileUpload::make('attachment')
    ->image()
    ->optimize('webp'),

Resizing images

You may also want to resize an image by passing in a percentage you would like to reduce the image by. This will also maintain aspect ratio.

E.g. I'd like to reduce my image (1280px x 720px) by 50%:

use Filament\Forms\Components\FileUpload;

FileUpload::make('attachment')
    ->image()
    ->resize(50),

Uploaded image size is 640px x 360px.

You can do the same using SpatieMediaLibraryFileUpload:

use Filament\Forms\Components\SpatieMediaLibraryFileUpload;

SpatieMediaLibraryFileUpload::make('attachment')
    ->image()
    ->resize(50),

Add maximum width and/or height

You can also add a maximum width and/or height to the image. This will resize the image to the maximum width and/or height, maintaining the aspect ratio.

use Filament\Forms\Components\FileUpload;

FileUpload::make('attachment')
    ->image()
    ->maxImageWidth(1024)
    ->maxImageHeight(768),

Combining methods

You can combine these two methods for maximum optimization.

use Filament\Forms\Components\FileUpload;

FileUpload::make('attachment')
	->image()
	->optimize('webp')
	->maxImageWidth(1024)
	->maxImageHeight(768)
	->resize(50),
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;

SpatieMediaLibraryFileUpload::make('attachment')
    ->image()
	->optimize('webp')
	->maxImageWidth(1024)
	->maxImageHeight(768)
    ->resize(50),

Multiple images

You can also do this with multiple images - all images will be converted to the same format and reduced with the same percentage passed in. Just chain on multiple() to your upload:

use Filament\Forms\Components\FileUpload;

FileUpload::make('attachment')
    ->image()
	->multiple()
	->optimize('jpg')
    ->resize(50),
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;

SpatieMediaLibraryFileUpload::make('attachment')
    ->image()
	->multiple()
	->optimize('jpg')
    ->resize(50),

Examples

Before

After

Debugging

  • If you see a 'not found' exception, including "Method optimize" or "Method resize", ensure you run composer update so that your lock file is in sync with your composer.json.

  • You might see a 'Waiting for size' message and an infinite loading state on the component and the likely cause of this is a CORS issue. This can be quickly be resolved by ensuring you are serving and upload images from the same domain. Check your Javascript console for more information.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

joshembling/image-optimizer 适用场景与选型建议

joshembling/image-optimizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 163.61k 次下载、GitHub Stars 达 113, 最近一次更新时间为 2023 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 joshembling/image-optimizer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 163.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 113
  • 点击次数: 26
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 113
  • Watchers: 5
  • Forks: 39
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-23