Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.
/ laravel-image Public archive

Basic image upload and thumbnail management package for laravel 5+

License

Notifications You must be signed in to change notification settings

ankitpokhrel/laravel-image

 
 

Repository files navigation

LARAVEL IMAGE

Latest Version Scrutinizer Code Quality Travis Build Code Coverage Software License Total Downloads

Basic image upload and thumbnail management package for laravel 5+. This package uses Glide library from the php league for on-demand image manipulation.

Installation

For 5.1 use 5.1 branch.
For 5.2 use 5.2 branch.
For 5.3 use master branch.

Pull the package via composer

composer require "ankitpokhrel/laravel-image":"5.2.x-dev"

Include the service provider within config/app.php.

AnkitPokhrel\LaravelImage\ImageUploadServiceProvider::class

Finally publish the configuration

php artisan vendor:publish --provider="AnkitPokhrel\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 $image;

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

        //set properties for file upload
        $this->image = $image;
        $this->image->setUploadField('image'); //default is image
        $this->image->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->image->upload()) {
        //image is uploaded, get uploaded image info
        $uploadedFileInfo = $this->image->getUploadedFileInfo();

        ...
        //do whatever you like with the information
        $input = array_merge($input, $uploadedFileInfo);
    } else {
        //get validator object
        $validator = $this->image->getValidationErrors();
        
        //get error messages
        $errors = $validator->messages()->all();
    }

    ...
}

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

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

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

    ...
}

Customizing upload path

Sometime you may want to group uploaded image or even store image somewhere else other than the public folder. You can do it by setting base path. For example, this settings below will store images inside public/uploads/user-images/users/ directory.

//set base path
$this->file->setBasePath('uploads/user-images/');

//set upload folder
$this->file->setUploadFolder('users');

If you want to upload image in other places other than public folder, you can provide second parameter as false to the base path method.

//set absolute base path
$this->file->setBasePath('/absolute/path/to/your/folder/uploads/', true);

//set upload folder
$this->file->setUploadFolder('users');

This will upload image to /absolute/path/to/your/folder/uploads/users folder. Make sure that the folder has proper permission to store the images.

Using blade directive to display images

Display full image

@laravelImage($uploadDir, $imageName)

Create image of custom size at runtime

<-- @laravelImage(uploadDir, imageName, width, height) -->
@laravelImage($uploadDir, $imageName, 300, 200)

Options & attributes

<-- @laravelImage(uploadDir, imageName, width, height, options, attributes) -->
@laravelImage($uploadDir, $imageName, 300, 200, [
    'fit' => 'crop-top-left',
    'filt' => 'sepia'
], [
    'alt' => 'Alt text of an image',
    'class' => 'custom-class'
])

Options can be any glide options. See thephpleague/glide for more info on options.

Displaying image without blade

Image source should be in the format laravelimage.routePath/uploadDir/image?options, where laravelimage.routePath is from configuration file. So if you set your routePath to cache, the image url will be something like this.

<img src="/cache/{{ $uploadDir . $image  }}?w=128&fit=crop-center" alt="" />