定制 avnsh1111/laravel-image-optimizer 二次开发

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

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

avnsh1111/laravel-image-optimizer

最新稳定版本:v1

Composer 安装命令:

composer require avnsh1111/laravel-image-optimizer

包简介

A Laravel package that automatically optimizes images uploaded through the application.

README 文档

README

Latest Version on Packagist Total Downloads

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 to true if you want to store just the file name in the database. Set to false if 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 to true if you want to keep the original image file when updating or deleting a record. Set to false if 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.

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-24