PHPackages                             benallfree/laravel-stapler-images - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. benallfree/laravel-stapler-images

ActiveLibrary[Image &amp; Media](/categories/media)

benallfree/laravel-stapler-images
=================================

Extend laravel-stapler with an Image model.

2.2.0(8y ago)114915MITPHPPHP &gt;=5.5.0

Since Dec 7Pushed 8y ago1 watchersCompare

[ Source](https://github.com/benallfree/laravel-stapler-images)[ Packagist](https://packagist.org/packages/benallfree/laravel-stapler-images)[ RSS](/packages/benallfree-laravel-stapler-images/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (15)Used By (0)

Laravel Stapler Images
======================

[](#laravel-stapler-images)

This Laravel 5 package builds upon [codesleeve/laravel-stapler](https://github.com/CodeSleeve/laravel-stapler) and takes a different approach to attachment storage by storing attachments in a single table. Besides this normalized approach to attachment storage, it also handles images and image sizes.

There are several benefits to storing your attachments in a single table:

- Easier mainteannce - no additional migrations needed when new attachments are added
- Utilities and Laravel commands to handle attachments do not need to know specific column mames
- Normalized data is more organized and searchable
- Avoids duplication of images

Setup
-----

[](#setup)

Install

```
composer require benallfree/laravel-stapler-images

```

Add the service providers to `config/app.php`

```
BenAllfree\LaravelStaplerImages\LaravelStaplerImagesServiceProvider::class,
Codesleeve\LaravelStapler\Providers\L5ServiceProvider::class,

```

Optionally add an alias for the `\Image` and `\Attachment` classes in `config/app.php`

```
'Image' => BenAllfree\LaravelStaplerImages\Image::class,
'Attachment' => BenAllfree\LaravelStaplerImages\Attachment::class,

```

Publish the config

```
php artisan vendor:publish

```

Take a look at the config files in `config/laravel-stapler`. If you're not familiar with the config files, see the [basic Stapler config docs](https://github.com/CodeSleeve/stapler/blob/master/docs/configuration.md). I add `images.php` where you can control settings for this package. In particular, if you want to adjust the name of the table and the sizes of images created, you can do it here.

I like this setting for `config/laravel-stapler/filesystem.php`

```
'url' => '/i/:id_partition/:style/:filename',

```

Don't forget to migrate:

```
php artisan migrate

```

Basic Usage
-----------

[](#basic-usage)

Super easy. Let's add an avatar to our `User` model.

First, create a migration. In this case, let's do a simple `belongsTo` relationship.

```
Schema::table('users', function (Blueprint $table) {
  $table->integer('avatar_image_id');
});

```

I named it `avatar_image_id` because we want the field to be treated as an `BenAllfree\LaravelStaplerImages\Image` object so image processing happens. This gives us extra features like processing various image sizes. If we didn't need that, we could have named it `avatar_file_id` and it would be a `BenAllfree\LaravelStaplerImages\Attachment` instead.

Now, add it to the User table

```
use BenAllfree\LaravelStaplerImages\AttachmentTrait;

class User
{
  use AttachmentTrait;
}

```

Great. Now we can rock and roll. Saving will generate and save all the images on the spot.

```
// Create an image
$url = "http://www.gravatar.com/avatar/71137e6e1c94b72f162da3262b700017.png";
$user->avatar_image_path = $url;
$user->save();

```

The `$url` can be a file path too.

Next, we can recall a processed image URL. The MIME type is always `image/jpg` for these.

```
// Use an image
echo $user->avatar_image->url('thumb');

```

The following sizes exist by default:

```
'large' => '640x640#',
'featured' => '585x585#',
'medium' => '400x400#',
'thumb' => '180x180#',
'admin' => '100x100#',
'tiny' => '75x75#',

```

Forms and Input
---------------

[](#forms-and-input)

Given a field named `avatar_image` like above...

### In the view

[](#in-the-view)

```
{!! Form::open(['method'=>'post', 'files'=>true]) !!}
{!! Form::file('avatar_image') !!}
{!! Form::submit('Update') !!}
{!! Form ::close() !!}

```

### In the controller

[](#in-the-controller)

```
function do_postback(Request $r)
{
  $u = Auth::user();
  $u->update($r->input());
  $u->update($r->files()); // This is the important one
}

```

Magic Getters and Setters
-------------------------

[](#magic-getters-and-setters)

Given a database field `_file_id`

`_file` - A getter that returns an `Attachment` object for the given underlying ID `_file_path()` - A setter mutator that accepts a file path or URL and creates an `Attachment` object from it. If it can't find the file with the path specified, it will look in the `la_path` config setting, then in `la_path()`, then in `root_path()`.

Likewise, a database field `_image_id` will do the same thing for `Image`, except it will add image processing when the objects are created.

`_image` - A getter that returns an `Image` object for the given underlying ID `_image_path()` - A setter mutator that accepts a file path or URL and creates an `Attachment` object from it. If it can't find the file with the path specified, it will look in the `la_path` config setting, then in `la_path()`, then in `root_path()`.

`Image` is NOT a subclass of `Attachment`, so these should not be used interchangeably.

Caching and Performance
-----------------------

[](#caching-and-performance)

By default, `Image::from_url($url)` will check `$url` against the `original_file_name` column in the images table and will only fetch the image the first time it has to. If you want to force it, use `from_url($url, true)`.

Custom Image Sizes
------------------

[](#custom-image-sizes)

No problem, just go into `config/laravel-stapler-images.php` and make whatever sizes you want.

Reprocessing Images
-------------------

[](#reprocessing-images)

If you change sizes of images, you will need to reprocess existing images.

```
php artisan images:reprocess

```

Securing Images
---------------

[](#securing-images)

If you want your images to be served from within secure routes instead of directly available from the `public` folder, make these modifications:

Create the following file:

```
/storage/uploads/.gitkeep

```

In `config/laravel-stapler/filesystem.php` to change where the images are stored (outside the webroot):

```
'path' => ':app_root/storage/uploads:url',

```

Then, create a route like this and add whatever security you need:

```
Route::get('/images/{id}/{size}', function($id,$size) {
  $image = \Image::find($id);
  if(!$image)
  {
    App::abort(404);
  }

  $response = Response::make(
     File::get($image->image->path($size)),
     200
  );
  $response->header(
    'Content-type',
    'image/jpeg'
  );
  return $response;
});

```

Advanced Usage (Working directly with attachments)
--------------------------------------------------

[](#advanced-usage-working-directly-with-attachments)

If you have a pivot table or some other need to work directly with attachments:

```
$image = Image::from_url($url);
$att = Attachment::from_url($url);

```

Integrating with Laravel Administrator
--------------------------------------

[](#integrating-with-laravel-administrator)

Do you love [Laravel Administrator](https://github.com/FrozenNode/Laravel-Administrator) as much as I do? Sweet. Here's how you do it.

First, familiarize yourself with the `[location](http://administrator.frozennode.com/docs/field-type-image)` attribute of upload fields in Laravel Administrator.

### Step 1: Choose ONE location where Laravel Administrator will upload your files.

[](#step-1-choose-one-location-where-laravel-administrator-will-upload-your-files)

In `config/laravel-stapler/images.php`, there is an `la_path` that can be configured. The default is fine, but if you want to change it you may. Use the same location for ALL models in Laravel Administrator. Laravel Stapler Images will look in this config path for any uploads being saved. I suggest adding a `.gitkeep` to the path.

### Step 2: Configure your Laravel Administrator model, being careful to use the `config()` path you chose in Step 1.

[](#step-2-configure-your-laravel-administrator-model-being-careful-to-use-the-config-path-you-chose-in-step-1)

Configure `config/administrator/.php` as follows:

```
