Git Product home page Git Product logo

resizer's Introduction

Simple Image Resizer via Intervention

Under development.

This is mainly for resizing multiple sizes for a single image. You create a Resizeable (Image) model and the event system will resize it into the needed sizes you have setup in your configuration.

Requirements

This is designed for Laravel >= 5.1.

Install

Composer require that bad boy.

composer require lagbox/laravel-resizer

Add the Service Provider to config/app.php.

...
    'providers' => [
        ...
        lagbox\Resizer\ResizerServiceProvider::class,
    ],
...

Publish the config file and migration.

artisan vendor:publish --provider="lagbox\Resizer\ResizerServiceProvider"

Run the migration.

artisan migrate

If you want to make any configuration adjustments you can edit the config file config/resizer.php.

Make sure you have the 'public' disk that is setup in config/app/filesystems.php. Link storage/app/public to public/storage and create a folder for your images in there images. Make sure that directory has the correct permissions and you should be good to go. (You can adjust the disk and the path in the resizer.php config file.)

Eloquent Relationship

You can add a morphMany or morphOne relationship on your model that will have the Resizable Image(s):

public function image()
{
    return $this->morphOne(\lagbox\resizer\Resizable::class);
}

Upload

The Resizer can handle the upload for you. You just need to pass it the UploadedFile object you get from the request.

public function store(Request $request, Resizer $resizer)
{
    $image = $request->file('image');

    $filename = $resizer->handleUpload($image);

    // or if you want to specify the filename yourself

    $image = $request->file('image');

    $filename = $resizer->handleUpload($image, 'something.jpg');
    ...
}

Trait

You can use lagbox\Resizer\Traits\HasResizableImage trait if you would like a addImage($filename) method and a getResizableName() method. By default the getResizableName method is simply:

return $this->id .'-'. $this->slug;

You can override this method on your model to change the format of the filename used for the Resizable Image associated with your model.

If you have named your relationship method something other than image you can set a parameter on your model to adjust for that.

protected $resizableName = 'images';

// or

protected $resizableName = 'photo';

Upload and Trait methods

These methods can be used to help you with the upload and saving process.

public function store(Request $request, Resizer $resizer)
{
    $post = Post::create($request->all());

    $file = $request->file('image');

    $image = $post->addImage(
        $resizer->handleUpload($file, $post->getResizableName())
    );
}

Resizing

Creating a new Resizable Image is all you have to do. The resize will fire off via an event.

Deleting

Deleting the model will fire an event and cause the images to be deleted from the filesystem.

Queue

If you want to have the resizing happen in a queue job, you can set queue to true in the config.

resizer's People

Contributors

lagbox 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.