LaravelPackages.net
Acme Inc.
Toggle sidebar
proshore/laravel-image-upload

Easy image upload and thumbnail management

851
6
About proshore/laravel-image-upload

proshore/laravel-image-upload is a Laravel package for easy image upload and thumbnail management. It currently has 6 GitHub stars and 851 downloads on Packagist. Install it with composer require proshore/laravel-image-upload. Discover more Laravel packages by proshore or browse all Laravel packages to compare alternatives.

Last updated

LARAVEL IMAGE

Basic image upload and thumbnail management package for laravel5.

Installation

First, pull in the package through Composer.

"require": {
    "proshore/laravel-image-upload": "dev-master"
}

And then, if using Laravel 5, include the service provider within config/app.php.

'providers' => [
    LaravelImage\ImageUploadServiceProvider::class,
];

And, for convenience, add a facade alias to this same file at the bottom:

'aliases' => [
    'ImageHelper' => LaravelImage\Image::class,
];

Finally publish the configuration

php artisan vendor:publish --provider="LaravelImage\ImageUploadServiceProvider"

Configuration

You can add default thumbnail configuration to config/laravelimage.php.

Usage

Uploading Image

Within your controllers, get access to image upload service via dependency injection

class ContentsController extends Controller
{
    ...

    protected $file;

    /**
     * @param ImageUploadService $file
     */
    public function __construct(ImageUploadService $file)
    {
        ...

        //set properties for file upload
        $this->file = $file;
        $this->file->setUploadField('image'); //default is image
        $this->file->setUploadFolder('contents'); //default is public/uploads/contents
    }

    ...

And then, in your store or update method you can perform image upload

/**
 * Store method
 */
public function store(ContentRequest $request)
{
    $input = $request->all();

    if (Input::hasFile('image') && $this->file->upload()) {
        //file is uploaded, get uploaded file info
        $uploadedFileInfo = $this->file->getUploadedFileInfo();

        ...
        //do whatever you like with the information
        $input = array_merge($input, $uploadedFileInfo);
    }

    ...
}

/**
 * Update method
 */
public function update(Request $request, $id)
{
    ...

    if (Input::hasFile('image') && $this->file->upload()) {
        ...

        //remove old files
        if ( ! empty($model->file_path)) {
            $this->file->clean(public_path($model->file_path), true);
        }
    }

    ...
}

Using facade to display images

Display from already registered thumbnails. It will try to generate new thumb if registered thumbnail doesn't exist.

{!!
    \LaravelImage\LaravelImage::image($model->file_path, $model->image, [
        'alt' => $model->original_file_name,
        'size' => 'thumb'
    ])
!!}

Or, create image of custom size at runtime

{!!
    \LaravelImage\LaravelImage::image($model->file_path, $model->image, [
        'alt' => $model->original_file_name,
        'size' => [
            'custom' => ['w' => 300, 'h' => 200, 'crop' => true]
        ]
    ])
!!}

TODO

  • Image cache implementation
  • Complete documentation
Comments