avnsh1111/laravel-image-optimizer
Composer 安装命令:
composer require avnsh1111/laravel-image-optimizer
包简介
A Laravel package that automatically optimizes images uploaded through the application.
README 文档
README
Laravel Image Optimizer is a package that provides an easy way to optimize and resize images in your Laravel applications.
Installation
You can install the package via composer:
composer require avnsh1111/laravel-image-optimizer
Usage
To use the package, you need to create a model that uses the OptimizeImage trait. The trait provides a set of options for optimizing and resizing your images.
Here's an example of a model using the OptimizeImage trait:
use LaravelImageOptimizer\Traits\OptimizeImage; use Illuminate\Database\Eloquent\Model; class TestImage extends Model { use OptimizeImage; protected $fillable = ['name']; protected static $optimizeImages = [ 'name' => [ 'name_only' => true, 'base_path' => 'public/uploads/images/', 'quality' => 85, 'width' => 800, 'height' => 800, 'keep_files' => false // This value will be overridden by the $keep_files property ], ]; }
Options
name_only: Set totrueif you want to store just the file name in the database. Set tofalseif you want to store the full path.base_path: The base path where your images will be stored.quality: The quality of the optimized image (from 0 to 100).width: The maximum width of the optimized image.height: The maximum height of the optimized image.keep_files: Set totrueif you want to keep the original image file when updating or deleting a record. Set tofalseif you want to remove the image file when updating or deleting a record.
Example
Here's an example of how to use the package to optimize and resize an image:
use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Intervention\Image\Facades\Image; // Fake storage for testing Storage::fake('public'); // Create a test image $image = Image::make(UploadedFile::fake()->image('test-image.jpg', 2000, 2000)); $imagePath = 'public/uploads/images/test-image.jpg'; Storage::put($imagePath, (string)$image->encode()); // Check if the image exists in storage $this->assertTrue(Storage::exists($imagePath)); // Create a TestImage model $testImage = TestImage::create(['name' => 'test-image.jpg']); // Load the optimized image $optimizedImage = Image::make(Storage::get($imagePath)); // Check if the optimized image dimensions are correct $this->assertLessThanOrEqual(800, $optimizedImage->width()); $this->assertLessThanOrEqual(800, $optimizedImage->height());
Testing
To run the tests, execute the following command:
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email er.avinashrathod@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
avnsh1111/laravel-image-optimizer 适用场景与选型建议
avnsh1111/laravel-image-optimizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 avnsh1111/laravel-image-optimizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 avnsh1111/laravel-image-optimizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-24