PHPackages                             cbcaio/image-attacher - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. cbcaio/image-attacher

ActiveLibrary[File &amp; Storage](/categories/file-storage)

cbcaio/image-attacher
=====================

Add uploaded images to models in an easy-to-use and fast approach. Process and save modified versions of images, like avatar, thumbnail, different sizes, you decide. Write images using flysystem.

1.0.1(10y ago)12551[1 issues](https://github.com/CbCaio/Image-Attacher/issues)MITPHP

Since Jan 20Pushed 10y agoCompare

[ Source](https://github.com/CbCaio/Image-Attacher)[ Packagist](https://packagist.org/packages/cbcaio/image-attacher)[ RSS](/packages/cbcaio-image-attacher/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (6)Versions (2)Used By (0)

Image Attacher
==============

[](#image-attacher)

[![License](https://camo.githubusercontent.com/9acb6e26215b5b1f952b90473f5f442dfc5fc8e2ced17513f0cef927aee71c56/68747470733a2f2f706f7365722e707567782e6f72672f63626361696f2f696d6167652d61747461636865722f6c6963656e7365)](LICENSE.md)[![Quality Score](https://camo.githubusercontent.com/a98292d13d2bd6d8e5ab76d2670a334a110dcd35653de46da8b78ed5a81a4a18/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f43624361696f2f496d6167652d41747461636865722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/CbCaio/Image-Attacher)[![Build Status](https://camo.githubusercontent.com/1bf61cf6bc7b65c86d892c9a393825b22c6934f527eda13a9901d8c85989a98e/68747470733a2f2f7472617669732d63692e6f72672f43624361696f2f496d6167652d41747461636865722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/CbCaio/Image-Attacher)[![Latest Stable Version](https://camo.githubusercontent.com/1adcd36f199177fddb33ab0ce7eb218875144824ebd2d42b790fe94deb5d0924/68747470733a2f2f706f7365722e707567782e6f72672f63626361696f2f696d6167652d61747461636865722f762f737461626c65)](link-packagist)

[![Total Downloads](https://camo.githubusercontent.com/d92c7b98b302754b7d9421ec805c62bd382363048967bcabb029c8bfc8b881bb/68747470733a2f2f706f7365722e707567782e6f72672f63626361696f2f696d6167652d61747461636865722f646f776e6c6f616473)](link-packagist)[![Monthly Downloads](https://camo.githubusercontent.com/859f4f49faef98a9e0f43363fe271dc5e37d4219323b78cf9bb38b8cea8b3800/68747470733a2f2f706f7365722e707567782e6f72672f63626361696f2f696d6167652d61747461636865722f642f6d6f6e74686c79)](link-packagist)[![Daily Downloads](https://camo.githubusercontent.com/16054803a7a3fa8816e43f56fe1ab478d51be6ac9cd580054a99f202b9c2fe1d/68747470733a2f2f706f7365722e707567782e6f72672f63626361696f2f696d6167652d61747461636865722f642f6461696c79)](link-packagist)

This package uses polymorphic relationships to easily attach images to models. Basically you just need to use one of the package's traits in your model and you are good to go(see Usage). What happens in the background is that the models are linked to the images by a MorphOne relationship and every time you persist a image into the associated table the file is written automatically using the selected driver (using [Flysystem](https://github.com/GrahamCampbell/Laravel-Flysystem)), or updated if needed.

Install
-------

[](#install)

### 1 - Via Composer

[](#1---via-composer)

To get started with Image Attacher, add it to your `composer.json` file as a dependency:

```
$ composer require cbcaio/image-attacher
```

### 2 - Provider

[](#2---provider)

After installing the Image Attacher, register the `CbCaio\ImgAttacher\Providers\ImgAttacherServiceProvider`in your configuration file (`config/app.php`):

```
'providers' => [
    // Other service providers...

    CbCaio\ImgAttacher\Providers\ImgAttacherServiceProvider::class,
],

```

### 3 - Configuration

[](#3---configuration)

In order to publish the configuration files run the following command

```
$ php artisan vendor:publish
```

This command will create 3 new files:

1. `config/img-attacher.php` : this file holds the Image Attacher configurations.

    - `path_to_save`: defines where images will be saved, relatively to the driver and local specified in the `flysystem.php`. This path will be parsed before being used. The :`attribute` references information about the `attacherImage` model while the :`owner_class` and :`owner_id` are relative to the owner class of the relationship. These are needed to organize folders and also to certify that the right image will be deleted.

        IMPORTANT: This path should be exclusively used by the package, do not put other files in the same folder otherwise they can be deleted by mistake.
    - `processing_styles` and `processing_style_routines`: the 'routine' represents a sequence of 'styles' and its needed methods. Each 'style' saves a copy of the original image with the modifications specified in its method. They can be used to save different versions of your original image automatically when you add an image to a model (the model can can have different 'versions'/'styles' of same image with only one relationship).

        - For example, the following code will save the original image and also a 'thumbnail version' of the same image in their respective parsed `path_to_save` with :`style` substituted by `original_style` and `thumbnail`:

            ```
               $model->addImage(uploaded_file,'default_routine');
               ....
                'default_routine' =>
                [
                    'original_style' => function ($image) {
                        return $image;
                    },
                    'thumbnail' => function ($image) {
                       $image->resize(null, 500, function ($constraint) {
                           $constraint->aspectRatio();
                           $constraint->upsize();
                       });
                       return $image;
                    },
                ]

            ```
2. `database/migrations/2016_01_12_000000_create_attacher_images_table` : this is a migration file to create the table which will hold the reference to your models in the MorthOne relationship and the information about the images.
3. `config/flysystem.php` : this file is relative to the [Flysystem](https://github.com/GrahamCampbell/Laravel-Flysystem). How and where your files will be written is defined here. It is important to say that this package is only tested using the 'local' driver.

Usage
-----

[](#usage)

To start attaching images to your models you just need to use one of the available traits (currently only `hasImage`) in the model.

```
 class RandomModel extends Model
 {
     use hasImage;
 }

```

### 1 - Basic of using the hasImage trait

[](#1---basic-of-using-the-hasimage-trait)

##### Adding an image to the model from an uploaded file.

[](#adding-an-image-to-the-model-from-an-uploaded-file)

```
```php
    $upload = Input::file('image');
    $model->addImage($upload);
     // Directly from $request
    $model->addImage($request->file('image'));
     // With parameters
    $model->addImage($request->file('image', 'processing_style_routine','newfilename.jpg'));
```

```

##### Retrieving an image from the model.

[](#retrieving-an-image-from-the-model)

```
```php
    $image = $user->getImage();
      // Path is relative
    $image->getPath('original_style);
    $image->getPath('thumbnail);

      // Url includes full path
    $image->getUrl('original_style);
    $image->getUrl('thumbnail);
```

```

##### Adding another image

[](#adding-another-image)

```
```php
    // The same as adding, the package will identify if the model already has an image, delete the previous
    images and update the relationship.
    $upload = Input::file('image2');
    $model->addImage($upload);
```

```

##### Deleting image and all the its styles

[](#deleting-image-and-all-the-its-styles)

```
```php
    $model->deleteImage();
```

```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email :author\_email instead of using the issue tracker.

Credits
-------

[](#credits)

- [CbCaio](https://github.com/CbCaio)
- [vinicius73](https://github.com/vinicius73)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3814d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5702563?v=4)[Caio C. Bolognani](/maintainers/cbcaio)[@cbcaio](https://github.com/cbcaio)

---

Top Contributors

[![cbcaio](https://avatars.githubusercontent.com/u/5702563?v=4)](https://github.com/cbcaio "cbcaio (6 commits)")

---

Tags

Flysystemlaravelimagesuploadlaravel5cbcaioattacher

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cbcaio-image-attacher/health.svg)

```
[![Health](https://phpackages.com/badges/cbcaio-image-attacher/health.svg)](https://phpackages.com/packages/cbcaio-image-attacher)
```

###  Alternatives

[unisharp/laravel-filemanager

A file upload/editor intended for use with Laravel 5 to 10 and CKEditor / TinyMCE

2.1k3.4M81](/packages/unisharp-laravel-filemanager)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[api-platform/laravel

API Platform support for Laravel

59156.3k11](/packages/api-platform-laravel)[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1232.2k16](/packages/fleetbase-core-api)[outerweb/image-library

Store and link files to your models

1113.8k3](/packages/outerweb-image-library)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
