dev-master
9999999-devProvides functionality for attach files to model and manipulate if file is image
MIT
The Requires
by Alexander Kireev
laravel s3 image ftp rackspace attach
Wallogit.com
2017 © Pedro Peláez
Provides functionality for attach files to model and manipulate if file is image
Provides functionality for attach files to model and manipulate if file is image, (*1)
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)
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]) !!}
Provides functionality for attach files to model and manipulate if file is image
MIT
laravel s3 image ftp rackspace attach