rainetro/laravel-imagebox

Use images with Eloquent models in Laravel

0.1.8 2023-07-02 14:27 UTC

This package is auto-updated.

Last update: 2024-10-01 00:11:10 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Use images with Eloquent models in Laravel. This package provides a simple way to attach images to Eloquent models to use in your web applications.

This package is inspired by spatie/laravel-medialibrary.

Installation

You can install the package via composer:

composer require rainetro/laravel-imagebox

You can publish and run the migrations with:

php artisan vendor:publish --provider="Rainet\ImageBox\ImageBoxServiceProvider" --tag="migrations"
php artisan migrate

You can (optionally) publish the config file with:

php artisan vendor:publish --provider="Rainet\ImageBox\ImageBoxServiceProvider" --tag="config"

This is the contents of the published config file:

return [
    'disk' => env('IMAGE_BOX_DISK', 'public'),
    'queue_connection_name' => env('QUEUE_CONNECTION', 'sync'),
    'queue_name' => '',
    'model' => \Rainet\ImageBox\Box\Models\Image::class,
    'folder_generator' => \Rainet\ImageBox\Box\FolderGenerator\DefaultFolderGenerator::class,
];

Usage

Attach images to your models

Ensure that your model implements the ImageBoxInterface. Add the ImageBoxTrait trait to the model. Implement the registerImageCollections() method. This method is used to define the image collections for your model. You can define one or more image collections by chaining the addImageCollection() method. Implement the registerImageCollectionConversions() method. This method is used to define the conversions for your image collections. Inside this method, you can chain the addConversion() method to add conversions to a specific image collection.

use Illuminate\Database\Eloquent\Model;
use Rainet\ImageBox\ImageBoxInterface;
use Rainet\ImageBox\ImageBoxTrait;

class Post extends Model implements ImageBoxInterface
{
    use ImageBoxTrait;

    //  ...

    public function registerImageCollections(): void
    {
        $this
            ->addImageCollection('image')
            ->acceptsMimeTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp']);
    }

    public function registerImageCollectionConversions(): void
    {
        $this
            ->getImageCollection('image')
            ->addConversion('thumb')
            ->width(480)
            ->quality(90);

        // $this
        //     ->getImageCollection('image')
        //     ->addConversion('medium')
        //     ->width(600)
        //     ->height(600)
        //     ->quality(90);
    }
}

Use the image method to attach images to your model.

$post = Post::find(1);
$post
    ->addImage($request->file('image'))
    ->toCollection('image');

You can also use the image() or images() methods to retrieve the image(s).

$post->load('image');
$post->load('images');

Testing (Coming Soon)

We are actively working on implementing comprehensive testing for this package. Stay tuned for updates as we continue to improve the testing coverage.

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

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