sa Husoft / eloquent-image-mutator
图像上传解决方案之一。
Requires
- php: >=5.5.9
- imagine/imagine: >=0.6.3
- laravel/framework: ^5.1
README
将图像与模型相关联总是很麻烦。Eloquent Image Mutator 为 Eloquent 模型提供了一个简单的修改器,用于保存和检索图像。
使用模型存储图像
通过表单上传
$user->profile_picture = \Input::file('image');
$user->save();
或者
通过公共 URL 上传(我们将为您下载并存储图像)
$user->profile_picture = https://scontent.fbom1-2.fna.fbcdn.net/hprofile-xaf1/v/t1.0-1/p160x160/11659393_10207093577848521_887484828555984342_n.jpg?oh=089042c5f4afa05e0dcbde51130c0eea&oe=56689CB9;
$user->save();
或者
复制已存在的图像对象
$user->profile_picture = $user->photo_one;
$user->save();
注意:
$user->photo_one 应该添加到 protected $image_fields
并且它的模型应该使用 EloquentImageMutatorTrait;
使用模型检索图像
$user->profile_picture->thumbnail->url
$user->profile_picture->xsmall->url
$user->profile_picture->small->url
$user->profile_picture->profile->url
$user->profile_picture->medium->url
$user->profile_picture->large->url
$user->profile_picture->original->url
您甚至可以访问高度和宽度
$user->profile_picture->large->height
$user->profile_picture->large->width
例如(在 blade 文件中):
<img src="{{ $user->profile_picture->profile->url }}" />
更新或删除图像
如果您用新的图像更新字段,则系统将删除旧图像。
或者
如果您从表中删除记录,则图像将被删除。
安装
将以下行添加到 composer.json 的 require 部分
{ "require": { "sahusoftcom/eloquent-image-mutator": "dev-master" } }
设置
- 在 /config/app.php 中,将以下内容添加到 providers
SahusoftCom\EloquentImageMutator\EloquentImageMutatorProvider::class,
-
运行 php artisan vendor:publish。
-
在 /config/image.php 中,您将获得默认的目标文件夹。
storage/app/uploads
如果您想使用此目标文件夹作为上传文件夹,请确保创建必要的目录,例如 storage/app/uploads。
-
在 public 文件夹中,您应该有一个指向上传文件夹目标位置的名为 uploads 的软链接。您可以在 public 中创建一个软链接。转到您项目的 public 文件夹
ln -s relative-path-to-destination-folder uploads
如何使用
- 您要存储图像的字段类型应该是 text。
- 您应该在模型中使用 EloquentImageMutatorTrait。
- 定义您想要包含在图像上传中的列。
例如
```
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use SahusoftCom\EloquentImageMutator\EloquentImageMutatorTrait;
class User extends Model
{
use EloquentImageMutatorTrait;
/**
* The photo fields should be listed here.
*
* @var array
*/
protected $image_fields = ['profile_picture', 'cover_photo'];
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['profile_picture', 'cover_photo'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [];
.
.
.
}
```
然后您可以使用用户对象,就像使用属性一样使用它
示例
使用模型存储图像
$user->profile_picture = \Input::file('image');
$user->save();
使用模型检索图像
$user->profile_picture->thumbnail->url
$user->profile_picture->xsmall->url
$user->profile_picture->small->url
$user->profile_picture->profile->url
$user->profile_picture->medium->url
$user->profile_picture->large->url
$user->profile_picture->original->url
感谢!
自定义
您可以自定义存储图像的目标文件夹。要自定义,请转到 config/image.php。第 6 行
'assets_upload_path' => 'storage/app/uploads',
并将目标更改为您想要的文件夹。确保目标文件夹具有所有权限,并且 public 文件夹中的软链接指向目标文件夹。
====================================================
Eloquent Image Mutator 版本:1.*
将图像与模型相关联总是很麻烦。Eloquent Image Mutator 为 Eloquent 模型提供了一个简单的修改器,用于保存和检索图像。
使用模型存储图像
$user->profile_picture = \Input::file('image');
$user->save();
使用模型检索图像
$user->profile_picture->thumbnail
$user->profile_picture->xsmall
$user->profile_picture->small
$user->profile_picture->profile
$user->profile_picture->medium
$user->profile_picture->large
例如(在 blade 文件中):
<img src="{{ $user->profile_picture->profile }}" />
安装
将以下行添加到 composer.json 的 require 部分
{ "require": { "sahusoftcom/eloquent-image-mutator": "dev-master" } }
设置
- 在 /config/app.php 中,将以下内容添加到 providers
SahusoftCom\EloquentImageMutator\EloquentImageMutatorProvider::class,
-
运行 php artisan vendor:publish。
-
在 /config/image.php 中,您将获得默认的目标文件夹。
storage/app/uploads
如果您想使用此目标文件夹作为上传文件夹,请确保创建必要的目录,例如 storage/app/uploads。
-
在 public 文件夹中,您应该有一个指向上传文件夹目标位置的名为 uploads 的软链接。您可以在 public 中创建一个软链接。转到您项目的 public 文件夹
ln -s relative-path-to-destination-folder uploads
如何使用
- 您要存储图像的字段类型应该是 text。
- 您应该在模型中使用 EloquentImageMutatorTrait。
- 定义您想要包含在图像上传中的列。
例如
```
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use SahusoftCom\EloquentImageMutator\EloquentImageMutatorTrait;
class User extends Model
{
use EloquentImageMutatorTrait;
/**
* The photo fields should be listed here.
*
* @var array
*/
protected $image_fields = ['profile_picture', 'cover_photo'];
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'image';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['profile_picture', 'cover_photo'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [];
.
.
.
}
```
然后您可以使用用户对象,就像使用属性一样使用它
示例
使用模型存储图像
$user->profile_picture = \Input::file('image');
$user->save();
使用模型检索图像
$user->profile_picture->thumbnail
$user->profile_picture->xsmall
$user->profile_picture->small
$user->profile_picture->profile
$user->profile_picture->medium
$user->profile_picture->large
感谢!
自定义
您可以自定义存储图像的目标文件夹。要自定义,请转到 config/image.php。第 6 行
'assets_upload_path' => 'storage/app/uploads',
并将目标更改为您想要的文件夹。确保目标文件夹具有所有权限,并且 public 文件夹中的软链接指向目标文件夹。