PHPackages                             salopot/laravel-attach-image - 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. salopot/laravel-attach-image

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

salopot/laravel-attach-image
============================

Provides functionality for attach files to model and manipulate if file is image

0481PHP

Since Nov 23Pushed 9y ago1 watchersCompare

[ Source](https://github.com/salopot/laravel-attach-image)[ Packagist](https://packagist.org/packages/salopot/laravel-attach-image)[ RSS](/packages/salopot-laravel-attach-image/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel attach image
====================

[](#laravel-attach-image)

Provides functionality for attach files to model and manipulate if file is image

Features
--------

[](#features)

- Use laravel [Filesystem / Cloud Storage](https://laravel.com/docs/filesystem) for store attached data. Now support: local, ftp, Amazon S3, Rackspace, DropBox ...
- Any image manipulation supported by [Intervention Image](http://image.intervention.io/getting_started/installation)
- Possibility process attached image formats on attach, on get content, on the fly (processed format not store)
- Return image as data url
- Automatic delete attached files when delete model

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Require this package with composer using the following command:

```
composer require salopot/laravel-attach-image "dev-master"

```

or add

```
"salopot/laravel-attach-image": "dev-master"

```

to the require section of your `composer.json` file.

After updating composer, configure [image processor](http://image.intervention.io/getting_started/installation#laravel) if it not used before

Configure app/filesystems.php add item "attach" to "disks" section

Local sample:

```
'attach' => [
   'driver' => 'local',
   'root'   => base_path('public'),
   'baseUrl' => php_sapi_name() != 'cli' ?  asset('') : config('app.url'),
]

```

Amazon S3 sample:

```
'attach' => [
    'driver' => 's3',
    'key'    => 'your-key',
    'secret' => 'your-secret',
    'region' => 'your-region', //'eu-central-1' for Frankfurt
    'bucket' => 'your-bucket', //'testattach'
    'baseUrl' => 'https://s3.eu-central-1.amazonaws.com/testattach/'
 ]

```

Usage
-----

[](#usage)

Use string fields in DB to store attached data relative path:

```
Schema::create('books', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('sample');
    $table->string('cover');
    $table->timestamps();
});

```

Extend eloquent model:

```
use Illuminate\Database\Eloquent\Model;
use App\Helpers\FileAttach;
use App\Helpers\ImageAttach;

class Book extends Model
{
    protected $fillable = ['name', 'cover', 'sample'];

    protected $_sample;
    public function getSampleAttribute() {
        return $this->_sample ? : $this->_sample = new FileAttach($this, 'sample');
    }

    public function setSampleAttribute($value) {
        $this->getSampleAttribute()->attachFile($value);
    }

    protected $_cover;
    public function getCoverAttribute() {
        return $this->_cover ? : $this->_cover = new ImageAttach($this, 'cover', [
            'list' => [
                'on' => ImageAttach::PT_ATTACH,
                'process' => function($image, $imageAttach) {
                    $width = 100;
                    $height = 100;
                    return $image
                        ->resize($width, $height, function($constraint) {
                            $constraint->aspectRatio();
                        })
                        //->resizeCanvas($width, $height);
                        ->greyscale();
                },
            ],
            'thumb' => [
                'on' => ImageAttach::PT_ATTACH,
                'process' => function($image, $imageAttach) {
                    return $image
                        ->widen(500)
                        ->text('Sample text', (int)($image->width() / 2), (int)($image->height() / 2), function($font) {
                            $font->align('center');
                            $font->valign('center');
                            $font->color('#ff0000');
                        });
                },
            ],
        ]);
    }

    public function setCoverAttribute($value) {
        $this->getCoverAttribute()->attachFile($value);
    }

    public static function boot() {
        parent::boot();

        static::deleting(function($model) {
            /*
            $model->sample->clearData();
            $model->cover->clearData();
            */
            foreach($model->getMutatedAttributes() as $attribute) {
                if ($model->{$attribute} instanceof FileAttach) {
                    $model->{$attribute}->clearData();
                }
            }
            return true;
        });
    }

```

And now you can use model basic fill methods to attach uploaded files:

```
class BookController extends Controller
{
...
    public function update(Request $request, $id)
    {
        $model = Book::findOrFail($id);
        //clear attach data functionality
        if ($request->has('clear_attach')) {
            if ($model->{$request->clear_attach} instanceof FileAttach) {
                $model->{$request->clear_attach}->clear();
                $model->save();
                return redirect()->to($this->getRedirectUrl())->withInput($request->input());
            }
        }
        $this->validate($request, [
            'name' => 'required|string|max:255',
            'sample' => 'mimes:pdf',
            'cover' => 'mimes:jpeg,png,bmp',
        ]);
        $model->fill($request->all())->save(); //store uploaded file to model
        return redirect()->route('book.show', ['id' => $id]);
    }

    public function destroy($id)
    {
        $model = Book::findOrFail($id);
        $model->delete();
        return redirect()->route('book.index');
    }

```

Simple view example:

```
{!! Form::open(['route' => 'book.store', 'files'=>true]) !!}

{{ csrf_field() }}

    {!! Form::label('name', 'Name', ['class' => 'control-label']) !!}
    {!! Form::text('name', null, ['class' => 'form-control']) !!}
    @if ($errors->has('name'))
        {{ $errors->first('name') }}
    @endif

    {!! Form::label('sample', 'Sample', ['class' => 'control-label']) !!}
    @if(isset($model) && $model->sample->attached())

                Delete

    @else
        {!! Form::file('sample', ['accept'=>'.pdf']) !!}
        @if ($errors->has('sample'))
            {{ $errors->first('sample') }}
        @endif
    @endif

    {!! Form::label('cover', 'Cover', ['class' => 'control-label']) !!}
    @if(isset($model) && $model->cover->attached())

                Delete

    @else
        {!! Form::file('cover', ['accept'=>'.png,.bmp,.jpg,.jpeg']) !!}
        @if ($errors->has('cover'))
            {{ $errors->first('cover') }}
        @endif
    @endif

```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/de866b61b006a3510267758ed2fcc6396fdad0eca7d09750d6aed2d9807f70f0?d=identicon)[salopot](/maintainers/salopot)

---

Top Contributors

[![salopot](https://avatars.githubusercontent.com/u/6479366?v=4)](https://github.com/salopot "salopot (10 commits)")

### Embed Badge

![Health badge](/badges/salopot-laravel-attach-image/health.svg)

```
[![Health](https://phpackages.com/badges/salopot-laravel-attach-image/health.svg)](https://phpackages.com/packages/salopot-laravel-attach-image)
```

PHPackages © 2026

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