2017 © Pedro Peláez
 

library laravel-attach-image

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

image

salopot/laravel-attach-image

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

  • Wednesday, November 23, 2016
  • by salopot
  • Repository
  • 1 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 8 % Grown

The README.md

Laravel attach image

Provides functionality for attach files to model and manipulate if file is image, (*1)

Features

  • Use laravel Filesystem / Cloud Storage for store attached data. Now support: local, ftp, Amazon S3, Rackspace, DropBox ...
  • Any image manipulation supported by Intervention Image
  • 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

The preferred way to install this extension is through composer., (*2)

Require this package with composer using the following command:, (*3)

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

or add, (*4)

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

to the require section of your composer.json file., (*5)

After updating composer, configure image processor if it not used before, (*6)

Configure app/filesystems.php add item "attach" to "disks" section, (*7)

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/' ], (*8)

Usage

Use string fields in DB to store attached data relative path:, (*9)

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

Extend eloquent model:, (*10)

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:, (*11)

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:, (*12)

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

The Versions

23/11 2016

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

by Alexander Kireev

laravel s3 image ftp rackspace attach