Skip to content

konnco/laravel-ekomers

Repository files navigation

Laravel Onimage

Build Status Latest Stable Version Total Downloads Latest Unstable Version License StyleCI

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.

Installation

composer require composer require konnco/laravel-onimage
php artisan vendor:publish
php artisan migrate

Configuration

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 :

  1. multiple these is used to define multiple image into field.
  2. sizes:configsize1,configsize2 these is used to define current field is going to resize into config size that you define in config/onimage.php.
  3. nullable these is used to define image field can be nulled.

Quick Example

Upload your image

$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();

Upload Type

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)

Authors

Releases

No releases published

Packages

No packages published

Languages