承接 shurik2k5/yii2-upload-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

shurik2k5/yii2-upload-behavior

Composer 安装命令:

composer require shurik2k5/yii2-upload-behavior

包简介

Improved version Upload behavior for Yii 2 by mohorev/yii2-upload-behavior

README 文档

README

Build Status Total Downloads Scrutinizer Code Quality Code Coverage

Upload behavior for Yii 2

This behavior automatically uploads file and fills the specified attribute with a value of the name of the uploaded file.

In this behaviour added ability to load file from URL and local files, and attach behavior for two or more attributes.

This is an enhanced version of https://github.com/mohorev/yii2-upload-behavior/

Installation

The preferred way to install this extension via composer.

Either run

php composer.phar require --prefer-dist shurik2k5/yii2-upload-behavior "*"

or add this code line to the require section of your composer.json file:

"shurik2k5/yii2-upload-behavior": "*"

Usage

Upload file from input forms

Attach the behavior in your model:

class Document extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['file', 'file', 'extensions' => 'doc, docx, pdf', 'on' => ['insert', 'update']],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::class, [ 'id' => 'id_category' ]);
    }

    /**
     * @inheritdoc
     */
    function behaviors()
    {
        return [
            [
                'class' => \mohorev\file\UploadBehavior::class,
                'attribute' => 'file',
                'scenarios' => ['insert', 'update'],
                'path' => '@webroot/upload/docs/{category.id}',
                'url' => '@web/upload/docs/{category.id}',
            ],
        ];
    }
}

Set model scenario in controller action:

class Controller extends Controller
{
    public function actionCreate($id)
    {
        $model = $this->findModel($id);
        $model->setScenario('insert'); // Note! Set upload behavior scenario.
        
        ...
        ...
    }
}

Example view file:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?= $form->field($model, 'image')->fileInput() ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

Upload file from url

To load file from url

$model = new Document();
$model->setScenario('update'); //Use scenarion for validation and load file
$model->uploadFromUrl('file', 'https://i.ytimg.com/vi/yfJbKba5xYM/maxresdefault.jpg');
$model->save();

Upload file from file

To load file from local file

$model = new Document();
$model->setScenario('update'); //Use scenarion for validation and load file
$model->uploadFromFile('file', \Yii::getAlias('@webroot/images/02.jpg'));
$model->save();

Get path in you application

Get upload url

$model->getUploadUrl('file');

Get upload path

$model->getUploadPath('file');

Upload image and create thumbnails

Thumbnails processing requires yiisoft/yii2-imagine to be installed.

Attach the behavior in your model:

class User extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['image', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => \mohorev\file\UploadImageBehavior::class,
                'attribute' => 'image',
                'scenarios' => ['insert', 'update'],
                'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
                'path' => '@webroot/upload/user/{id}',
                'url' => '@web/upload/user/{id}',
                //if need create all thumbs profiles on image upload
                'createThumbsOnSave' => true,
                //if need create thumb for one profile only on request by getThumbUploadUrl() method
                'createThumbsOnRequest' => true,
                //if you want to remove original upload file after images thumbs was generated
                'deleteOriginalFile' => true,
                'thumbs' => [
                    'thumb' => ['width' => 400, 'quality' => 90],
                    'preview' => ['width' => 200, 'height' => 200],
                    'news_thumb' => ['width' => 200, 'height' => 200, 'bg_color' => '000'],
                ],
            ],
        ];
    }
}

Remove upload image and all thumbs

//remove image and all thumbs from 'photo' attribute
$model->deleteImage('photo');

Flexible configuration for path and URL generation

More flexible configuration for path and/or url behavior properties is that to use callbacks or array for defining path or url generation logic.

I. Via callbacks

/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        [
            'class' => \mohorev\file\UploadImageBehavior::class,
            'attribute' => 'image',
            'scenarios' => ['insert', 'update'],
            'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
            'path' => function ($model) {
                /** @var \app\models\UserProfile $model */
                $basePath = '@webroot/upload/profiles/';
                $path = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2));
                return $basePath . $path;
            },
            'url' => function ($model) {
                /** @var \app\models\UserProfile $model */
                $baseUrl = '@web/upload/profiles/';
                $path = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2));
                return $baseUrl . $path;
            },
            'thumbs' => [
                'thumb' => ['width' => 400, 'quality' => 90],
                'preview' => ['width' => 200, 'height' => 200],
                'news_thumb' => ['width' => 200, 'height' => 200, 'bg_color' => '000'],
            ],
        ],
    ];
}

II. Via array configuration by defining class and its static methods for path/URL generation

/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        [
            'class' => \mohorev\file\UploadImageBehavior::class,
            'attribute' => 'image',
            'scenarios' => ['insert', 'update'],
            'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
            'path' => [UserProfile::class, 'buildAvatarPath'],
            'url' => [UserProfile::class, 'buildAvatarUrl'],
            'thumbs' => [
                'thumb' => ['width' => 400, 'quality' => 90],
                'preview' => ['width' => 200, 'height' => 200],
                'news_thumb' => ['width' => 200, 'height' => 200, 'bg_color' => '000'],
            ],
        ],
    ];
}

/**
 * Define two static methos in your model for path and URL generation
 */ 
/**
 * @param \app\models\UserProfile|\yii\db\ActiveRecord $profile
 * @return string
 */
public static function buildAvatarPath(UserProfile $model)
{
    $basePath = '@webroot/upload/profiles/';
    $path = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2));

    return $basePath . $path;
}

/**
 * @param \app\models\UserProfile|\yii\db\ActiveRecord $profile
 * @return string
 */
public static function buildAvatarUrl(UserProfile $model)
{
    $baseUrl = '@web/upload/profiles/';
    $path = implode('/', array_slice(str_split(md5($model->id), 2), 0, 2));

    return $baseUrl . $path;
}

Example view file:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <div class="form-group">
        <div class="row">
            <div class="col-lg-6">
                <!-- Original image -->
                <?= Html::img($model->getUploadUrl('image'), ['class' => 'img-thumbnail']) ?>
            </div>
            <div class="col-lg-4">
                <!-- Thumb 1 (thumb profile) -->
                <?= Html::img($model->getThumbUploadUrl('image'), ['class' => 'img-thumbnail']) ?>
            </div>
            <div class="col-lg-2">
                <!-- Thumb 2 (preview profile) -->
                <?= Html::img($model->getThumbUploadUrl('image', 'preview'), ['class' => 'img-thumbnail']) ?>
            </div>
        </div>
    </div>
    <?= $form->field($model, 'image')->fileInput(['accept' => 'image/*']) ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

Behavior Options

  • attribute - The attribute which holds the attachment
  • scenarios - The scenarios in which the behavior will be triggered
  • instanceByName - Getting file instance by name, If you use UploadBehavior in RESTfull application and you do not need a prefix of the model name, set the property instanceByName = false, default value is false
  • path - the base path or path alias to the directory in which to save files.
  • url - the base URL or path alias for this file
  • generateNewName - Set true or anonymous function takes the old filename and returns a new name, default value is true
  • unlinkOnSave - If true current attribute file will be deleted, default value is true
  • unlinkOnDelete - If true current attribute file will be deleted after model deletion.
  • deleteEmptyDir - If true the empty directory will be deleted after model deletion, default value is true.

UploadImageBehavior additional Options

  • createThumbsOnSave - If true create all thumbs profiles on image upload
  • createThumbsOnRequest - If true create thumb only for profile request by getThumbUploadUrl('attribute', 'profile_name) method. If true recommend to set createThumbsOnSave to false
  • deleteOriginalFile - If true the original upload image will be deleted after images thumbs was generated, default value is false.
    Attention don't use with createThumbsOnRequest options, because thumbs generate on request (NOT after upload image) and after first profile thumb generation original file will be deleted!

Attention!

It is prefered to use immutable placeholder in url and path options, other words try don't use related attributes that can be changed. There's bad practice. For example:

class Track extends ActiveRecord
{
    public function getArtist()
    {
        return $this->hasOne(Artist::class, [ 'id' => 'id_artist' ]);
    }

    public function behaviors()
    {
        return [
            [
                'class' => \mohorev\file\UploadBehavior::class,
                'attribute' => 'image',
                'scenarios' => ['default'],
                'path' => '@webroot/uploads/{artist.slug}',
                'url' => '@web/uploads/{artist.slug}',
            ],
        ];
    }
}

If related model attribute slug will change, you must change folders' names too, otherwise behavior will works not correctly.

Authors

shurik2k5/yii2-upload-behavior 适用场景与选型建议

shurik2k5/yii2-upload-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 211 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 shurik2k5/yii2-upload-behavior 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 211
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 55
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-05-14