paulzi/yii2-file-behavior 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

paulzi/yii2-file-behavior

Composer 安装命令:

composer require paulzi/yii2-file-behavior

包简介

File store and image thumbs behavior for Yii2

README 文档

README

File store and image thumbs behavior for Yii2.

Packagist Version Total Downloads

Features

  • single and multiple file store for ActiveRecord
  • multiple image thumbs without extra db fields
  • user-friendly API
  • flexible class inheritance
  • support yii alias and different location of web/real path

Install

Install via Composer:

composer require paulzi/yii2-file-behavior:~1.2.0

or add

"paulzi/yii2-file-behavior" : "~1.2.0"

to the require section of your composer.json file.

Usage

Use FileBehavior in model and fill attributes option:

class Sample extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'path'  => '@webroot/files',
                'url'   => '@web/files',
                'attributes' => [
                    'file'  => [],
                    'files' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                    ],
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'types' => [
                            'original' => [1200, 1200],
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                    'images' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                        'item'  => [
                            'class' => 'paulzi\fileBehavior\Image',
                            'types' => [
                                'thm'  => [120, 120],
                            ],
                        ]
                    ],
                ],
            ],
        ];
    }
}

Set files

$model = Sample::findOne(1);
$file  = UploadedFile::getInstance($model, 'file');
$model->file->value = $file->tempName;
$model->save();

$model = Sample::findOne(2);
$files = UploadedFile::getInstances($model, 'images');
foreach ($files as $file) {
    $model->images[] = $file->tempName;
}
$model->save();

Get files

$model = Sample::findOne(1);
$url   = $model->file->url;
$path  = $model->file->path;

$model = Sample::findOne(2);
foreach ($model->images as $image) {
    echo $image->url;      // original image url
    echo $image->thm->url; // thm image url
}

Remove files

$model = Sample::findOne(1);
$model->file->value = null;
$model->save();

$model = Sample::findOne(1);
$model->files[2]->value = null;
$model->save();

$model = Sample::findOne(2);
$model->images->value = null;
$model->save();

Image salt

To generate a thumbnail file name is using a hash of the file name and type of thumbnail. If you need to protect the possibility of obtaining different types of thumbnail, set options salt by secret:

    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'attributes' => [
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'salt'  => 'secret',
                        'types' => [
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                ],
            ],
        ];
    }

Set options globally

You can set salt, path and url options globally by using Dependency Injection:

config\main.php:

    'aliases' => [
        '@cdnWeb' => 'http://s.example.com',
    ],

    'on beforeRequest' => function () {
        \Yii::$container->set('paulzi\fileBehavior\FileBehavior', [
            'path' => '@cdn\web\files',
            'url'  => '@cdnWeb\web\files',
        ]);
        \Yii::$container->set('paulzi\fileBehavior\Image', [
            'salt' => Yii::$app->params['salt'],
        ]);
    },

config\params-local.php:

    'salt' => 'secret salt',

Extending

You can extend classes for change path building function or change file storing.

By default, files are stores in {path}/{folder}/{12}/{12}/{1234567890abcdef1234567890ab}.{extension}

paulzi/yii2-file-behavior 适用场景与选型建议

paulzi/yii2-file-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 575 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-28