This package is designed to boost up your developing time in managing your image in Laravel framework.
This package based on the famous Intervention/Image
This package is still in alpha version, so the update may broke your application.
composer require composer require konnco/laravel-onimage
php artisan vendor:publish
php artisan migrate
you can find onimage configuration here. config/onimage.php
/*
|--------------------------------------------------------------------------
| Image Upload Drivers
|--------------------------------------------------------------------------
|
| define driver you should use to upload your image.
|
*/
'driver' => 'public',
/*
|--------------------------------------------------------------------------
| Available image sizes
|--------------------------------------------------------------------------
|
| define driver you should use to upload your image.
|
| size example original :
| * width
| * height
| * position
| * top-left
| * top
| * top-right
| * left
| * center (default)
| * right
| * bottom-left
| * bottom
| * bottom-right
|
*/
'sizes' => [
'original' => [null, null],
'more-size' => [width, height, position]
],
Add onimage traits into your model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class News extends Model {
use \Konnco\Onimage\Onimage;
}
and then define your field for images.
in this package we separate our image type into 2 section
- single (usually used for featured image)
- multiple (usually used for gallery image)
in your model define protected field named protected $imageAttributes
following example below :
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Konnco\Onimage\Onimage;
class News extends Model {
use Onimage;
protected $imageAttributes = [
'cover' => '',
'galleries' => 'multiple|sizes:original,square|nullable',
];
}
Available Rules :
multiple
these is used to define multiple image into field.sizes:configsize1,configsize2
these is used to define current field is going to resize into config size that you define inconfig/onimage.php
.nullable
these is used to define image field can be nulled.
$fruit = new Fruit();
$fruit->name = 'banana';
$fruit->cover = 'https://images.unsplash.com/photo-1562887250-9a52d844ad30?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2089&q=80';
$fruit->galleries = [
'https://images.unsplash.com/photo-1562887250-9a52d844ad30?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2089&q=80',
'https://images.unsplash.com/photo-1562887250-9a52d844ad30?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2089&q=80',
];
$fruit->save();
You can insert these types into onimage field :
- string - Path of the image in filesystem.
- string - URL of an image (allow_url_fopen must be enabled).
- string - Binary image data.
- string - Data-URL encoded image data.
- string - Base64 encoded image data.
- resource - PHP resource of type gd. (when using GD driver)
- object - Imagick instance (when using Imagick driver)
- object - Intervention\Image\Image instance
- object - SplFileInfo instance (To handle Laravel file uploads via Symfony\Component\HttpFoundation\File\UploadedFile)