Coder Social home page Coder Social logo

lloricode / laravel-imageable Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 283 KB

Painless uploading and managing images to your eloquent laravel models.

License: MIT License

PHP 100.00%
eloquent images uploader laravel models optimize spatie cache

laravel-imageable's Introduction

Laravel Imageable [ABANDONED]

please use this spatie/laravel-medialibrary

Latest Version on Packagist Build Status Total Downloads

  • Painless uploading and managing images to your eloquent laravel models.

Note: This project abstracts Spatie Image

Requirements

- CACHE_DRIVER=memcached (you can disable cache when need it)

Installation

composer require lloricode/laravel-imageable
  • add to your model
// ...
use Illuminate\Database\Eloquent\Model;
use Lloricode\LaravelImageable\Models\Traits\ImageableTrait;

class MyModel extends Model
{
    use ImageableTrait;
    
    
    // ...
    
  • Config
<?php

return [


    'migration' => [
        'table_name' => 'images',
    ],

    'cache' => [
        'enable' => true,
        'prefix' => 'imageable',
    ],
];
  • add this to your DatabaseSeeder to delete all test file in your storage while seeding data.
   use Lloricode\LaravelImageable\Models\Image;
        
    $folder = Image::PATH_FOLDER .'/';
    Storage::disk('local')->deleteDirectory($folder);
    Storage::disk('public')->deleteDirectory($folder);

Usage

        // files to be upload 
        $imageFiles = [
            //  instance of \Illuminate\Http\UploadedFile
            $imageFile1,
            $imageFile2,
        ];

        $myModel->uploads($imageFiles)// must array, 
            ->each([
                [
                    'size_name' => 'banner', // this will be use in calling image
                    'spatie' => function ($image) {

                        $image// abstract of spatie/image https://github.com/spatie/image
                        ->optimize()->width(100)-> // ....

                        return $image;
                    },
                ],
                [
                    'size_name' => 'thumbnail', // this will be use in calling image
                    'spatie' => function ($image) {

                        $image// abstract of spatie/image https://github.com/spatie/image
                        ->greyscale()-> // ....

                        return $image;
                    },
                ],
            ])->contentTypes([
                'image/png',
                'image/jpg',
                'image/jpeg',
            ])->disk('public')// any disk in config('filesystem) except cloud
            ->category('banner')// optional
            ->save(); // save mutiple 

Getter

    $images = $myModel->getImages($size_name = null, $category = null, $group = null); // a size_name use when uploading,
    <img href={{ $images->first()->source }} />
  • the images result of $myModel->getImages()
Illuminate\Support\Collection {#2947
  #items: array:4 [
    0 => {#2948
      +"size_name": "banner"
      +"category": "banner"
      +"group": "b2cec498052fe0c511af6fe24c06b7e2"
      +"client_original_name": "avatar.jpg"
      +"source_delete": "http://localhost/imageable/public-test-120-300"
      +"source": "http://localhost/storage/imageable/test-model/c4ca4238a0b923820dcc509a6f75849b/banner-43faf04b2c2b6cbf05c49b76cd6b7290.jpg"
    }
    1 => {#2946
      +"size_name": "thumbnail"
      +"category": "banner"
      +"group": "b2cec498052fe0c511af6fe24c06b7e2"
      +"client_original_name": "avatar.jpg"
      +"source_delete": "http://localhost/imageable/public-test-120-300"
      +"source": "http://localhost/storage/imageable/test-model/c4ca4238a0b923820dcc509a6f75849b/thumbnail-43faf04b2c2b6cbf05c49b76cd6b7290.jpg"
    }
    2 => {#2963
      +"size_name": "banner"
      +"category": "banner"
      +"group": "b2cec498052fe0c511af6fe24c06b7e2"
      +"client_original_name": "avatar.jpg"
      +"source_delete": "http://localhost/imageable/public-test-120-300"
      +"source": "http://localhost/storage/imageable/test-model/c4ca4238a0b923820dcc509a6f75849b/banner-778f3ef046040aafa2c71d803a5e41ba.jpg"
    }
    3 => {#2994
      +"size_name": "thumbnail"
      +"category": "banner"
      +"group": "b2cec498052fe0c511af6fe24c06b7e2"
      +"client_original_name": "avatar.jpg"
      +"source_delete": "http://localhost/imageable/public-test-120-300"
      +"source": "http://localhost/storage/imageable/test-model/c4ca4238a0b923820dcc509a6f75849b/thumbnail-778f3ef046040aafa2c71d803a5e41ba.jpg"
    }
  ]
}

Delete

$myModel->deleteImages($size_name = null, $category = null, $group = null);

# via http delete
# using `source_delete` return from collection

Regenerate

        $myModel->uploads()
            ->each([
                [
                    'size_name' => 'from', // this will be use in calling image
                    'spatie' => function ($image) {

                        // ....

                        return $image;
                    },
                ],
                [
                    'size_name' => 'to', // this will be use in calling image
                    'spatie' => function ($image) {

                         // ....

                        return $image;
                    },
                ],
                [
                    'size_name' => 'other', // this will be use in calling image
                    'spatie' => function ($image) {

                         // ....

                        return $image;
                    },
                ],
            ])
            ->regenerate('from'); // will regenerate `to` and`other`

laravel-imageable's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.