6reduk/eloquent-image-mutator

图像上传的解决方案。Fork 自 https://github.com/sahusoftcom/eloquent-image-mutator

v2.1.10 2016-05-10 16:40 UTC

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

并且其模型应该 use 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"
    }
}

设置

  1. 在 /config/app.php 中,将以下内容添加到 providers
SahusoftCom\EloquentImageMutator\EloquentImageMutatorProvider::class,
  1. 运行 php artisan vendor:publish

  2. 在 /config/image.php 中,您将获得默认目标文件夹。

    storage/app/uploads
    

    如果您想使用此目标文件夹作为上传文件夹,请确保创建所需的目录,例如 storage/app/uploads。

  3. 在公共文件夹中,您应该有一个指向上传文件夹目标的名为 uploads 的软链接。您可以通过在公共文件夹中创建一个软链接来完成此操作。转到您的项目公共文件夹并

    ln -s relative-path-to-destination-folder uploads
    

如何使用

  1. 您想要存储图像的字段应该是文本类型。
  2. 您应该在模型中使用 EloquentImageMutatorTrait。
  3. 定义您想要包含的图像上传的列。

例如

```
<?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',

并将目标更改为您想要的文件夹。确保目标文件夹具有所有权限,并且公共文件夹中的软链接指向目标文件夹。

====================================================

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"
    }
}

设置

  1. 在 /config/app.php 中,将以下内容添加到 providers
SahusoftCom\EloquentImageMutator\EloquentImageMutatorProvider::class,
  1. 运行 php artisan vendor:publish

  2. 在 /config/image.php 中,您将获得默认目标文件夹。

    storage/app/uploads
    

    如果您想使用此目标文件夹作为上传文件夹,请确保创建所需的目录,例如 storage/app/uploads。

  3. 在公共文件夹中,您应该有一个指向上传文件夹目标的名为 uploads 的软链接。您可以通过在公共文件夹中创建一个软链接来完成此操作。转到您的项目公共文件夹并

    ln -s relative-path-to-destination-folder uploads
    

如何使用

  1. 您想要存储图像的字段应该是文本类型。
  2. 您应该在模型中使用 EloquentImageMutatorTrait。
  3. 定义您想要包含的图像上传的列。

例如

```
<?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',

并将目标更改为您想要的文件夹。确保目标文件夹具有所有权限,并且公共文件夹中的软链接指向目标文件夹。