yashenkov / laravel-imagetrait
laravel 的 ImageTrait。上传/删除图片和缩略图,将路径存储在数据库中
dev-master
2017-10-31 08:10 UTC
This package is not auto-updated.
Last update: 2024-09-19 05:10:08 UTC
README
2017 年 Hacktoberfest
ImageTrait for Laravel 5.4
需求
- laravel 5.4^ (因为特质使用了 Laravel 包中的 Storage 和 Image 类)
特性
处理图片文件上传和数据库存储
- 在 CREATE
- 将图片文件存储在目标路径
- 生成名称
- 将路径存储在数据库中;
- 在 UPDATE
- 如果值为空,则删除图片文件并将数据库中的值设置为空
- 如果值不同,则存储不同的图片文件并更新数据库值。
安装 #1
composer require yashenkov/laravel-imagetrait
安装 #2
Download ImageTrait.php, than place it in app/Traits.
Replace namespase "Yashenkov\ImageTrait\Traits" by "App\Traits"
使用
在你的模型中,如果需要为具有图片属性的模型添加 ImageTrait,例如
<?php namespace App\Models; use App\Traits\ImageTrait; use Illuminate\Database\Eloquent\Model; class Product extends Model { use ImageTrait; ... }
然后您可以使用模型类中的特质方法作为图片属性的自定义修改器
<?php namespace App\Models; use App\Traits\ImageTrait; use Illuminate\Database\Eloquent\Model; class Product extends Model { use ImageTrait; public $disk = "uploads"; public $destination_path = "products"; public $imageWidth = 270; public $imageHeight = 270; /** * Mutators */ public function setImageAttribute($value, $attribute_name = 'image') { $this->uploadImageToDisk($value, $attribute_name, $this->disk, $this->destination_path, $this->imageWidth, $this->imageHeight); } }
##许可 MIT