定制 sahusoftcom/eloquent-image-mutator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sahusoftcom/eloquent-image-mutator

Composer 安装命令:

composer require sahusoftcom/eloquent-image-mutator

包简介

One solution for image uploads.

README 文档

README

Relating an image with a model is always a pain. Eloquent Image Mutator provides an easy mutator for Eloquent models to save and retrieve images.

Storing images with Model

Upload using a form

    $user->profile_picture = \Input::file('image');
    $user->save();

OR

Upload using a public URL (We will download and store the image for you)

    $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();

OR

Copy already present Image Object

	$user->profile_picture = $user->photo_one;
	$user->save();

note:-

The $user->photo_one should be a added to protected $image_fields

And its model should use EloquentImageMutatorTrait;

Retrieving images with Model

     $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

You can even access the height and width

	$user->profile_picture->large->height
	$user->profile_picture->large->width

Eg (In blade file):-

   <img src="{{ $user->profile_picture->profile->url }}" />

Update Or Delete Images

If you update a field with a new image the old image is deleted from the system.

OR

If you delete a record from a the table the image is deleted.

Installation

Add the following line to the require section of composer.json:

{
    "require": {
        "sahusoftcom/eloquent-image-mutator": "dev-master"
    }
}

Setup

  1. In /config/app.php, add the following to providers:
SahusoftCom\EloquentImageMutator\EloquentImageMutatorProvider::class,
  1. Run php artisan vendor:publish.

  2. In /config/image.php, you will have the default target folder.

    storage/app/uploads
    

    If you want to use this detination as the upload folder, Do make the directories required i.e., storage/app/uploads.

  3. In public folder you should have a soft-link pointing to the upload folder destination named uploads. You could do this by creating a soft link in public.Go to your projects public folder and

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

How to use

  1. The field against which you want to store the image should be of type text.
  2. You should use the EloquentImageMutatorTrait in the Model.
  3. Define the columns you want to inculde for image uploads.

For example:

```
<?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 = [];
    .
    .
    .
    
}
```

And to then with the user object you can just use it like a property

Example:

Storing images with Model

    $user->profile_picture = \Input::file('image');
    $user->save();

Retrieving images with Model

     $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

Thanks!

Customization

You could customize the target folder where the images are stored. For customizing goto config/image.php. Line no 6

'assets_upload_path' => 'storage/app/uploads',

and change the destination to your desired folder. Make sure the destination folder has all the permissions and the soft link in the public folder is pointing to the destionation folder.

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

Eloquent Image Mutator Version: 1.*

Relating an image with a model is always a pain. Eloquent Image Mutator provides an easy mutator for Eloquent models to save and retrieve images.

Storing images with Model

    $user->profile_picture = \Input::file('image');
    $user->save();

Retrieving images with Model

     $user->profile_picture->thumbnail
     $user->profile_picture->xsmall
     $user->profile_picture->small
     $user->profile_picture->profile
     $user->profile_picture->medium
     $user->profile_picture->large

Eg (In blade file):-

   <img src="{{ $user->profile_picture->profile }}" />

Installation

Add the following line to the require section of composer.json:

{
    "require": {
        "sahusoftcom/eloquent-image-mutator": "dev-master"
    }
}

Setup

  1. In /config/app.php, add the following to providers:
SahusoftCom\EloquentImageMutator\EloquentImageMutatorProvider::class,
  1. Run php artisan vendor:publish.

  2. In /config/image.php, you will have the default target folder.

    storage/app/uploads
    

    If you want to use this detination as the upload folder, Do make the directories required i.e., storage/app/uploads.

  3. In public folder you should have a soft-link pointing to the upload folder destination named uploads. You could do this by creating a soft link in public.Go to your projects public folder and

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

How to use

  1. The field against which you want to store the image should be of type text.
  2. You should use the EloquentImageMutatorTrait in the Model.
  3. Define the columns you want to inculde for image uploads.

For example:

```
<?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 = [];
    .
    .
    .
    
}
```

And to then with the user object you can just use it like a property

Example:

Storing images with Model

    $user->profile_picture = \Input::file('image');
    $user->save();

Retrieving images with Model

     $user->profile_picture->thumbnail
     $user->profile_picture->xsmall
     $user->profile_picture->small
     $user->profile_picture->profile
     $user->profile_picture->medium
     $user->profile_picture->large

Thanks!

Customization

You could customize the target folder where the images are stored. For customizing goto config/image.php. Line no 6

'assets_upload_path' => 'storage/app/uploads',

and change the destination to your desired folder. Make sure the destination folder has all the permissions and the soft link in the public folder is pointing to the destionation folder.

sahusoftcom/eloquent-image-mutator 适用场景与选型建议

sahusoftcom/eloquent-image-mutator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.41k 次下载、GitHub Stars 达 120, 最近一次更新时间为 2015 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「image」 「upload」 「laravel」 「photo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sahusoftcom/eloquent-image-mutator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sahusoftcom/eloquent-image-mutator 我们能提供哪些服务?
定制开发 / 二次开发

基于 sahusoftcom/eloquent-image-mutator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 123
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 120
  • Watchers: 21
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-14