bajadev/yii2-upload-behavior
Composer 安装命令:
composer require bajadev/yii2-upload-behavior
包简介
Yii2 file/image upload behavior for ActiveRecord
README 文档
README
This package is the set of two similar behaviors. The first one allows you to keep the uploaded file as-is. And the second one allows you to generate set of thumbnails for the uploaded image. Behaviors could be attached multiple times for different attributes.
Installation
The preferred way to install this extension is through composer.
Either run
composer bajadev/yii2-upload-behavior "*"
or add
"bajadev/yii2-upload-behavior": "*"
to the require section of your composer.json.
FileUploadBehavior
This behavior allow you to add file uploading logic with ActiveRecord behavior.
Usage
Attach the behavior to your model class:
public function behaviors()
{
return [
[
'class' => '\bajadev\upload\FileUploadBehavior',
'attribute' => 'fileUpload',
'filePath' => '@webroot/uploads/[[pk]].[[extension]]',
'fileUrl' => '/uploads/[[pk]].[[extension]]',
],
];
}
You can use Yii path aliases
like @app, @webroot, @web in your path template configuration.
You can also use additional placeholders in path templates.
Placeholder [[foobar]] will be replaced with appropriate placeholder value.
Possible path/url placeholders:
[[model]]- model class name[[pk]]- value of the primary key[[id]]- the same as[[pk]][[attribute_name]]- attribute value, for example[[attribute_ownerId]][[slug_attribute_name]]- generate attribute value by Inflector, for example[[attribute_ownerId]][[md5_attribute_name]]- generate md5 value, for example[[attribute_ownerId]][[id_path]]- id subdirectories structure (if model primary key is12345, placeholder value will be1/2/3/4/5[[basename]]- original filename with extension[[filename]]- original filename without extension[[extension]]- original extension
Add validation rule:
public function rules()
{
return [
['fileUpload', 'file'],
];
}
Setup proper form enctype:
$form = \yii\bootstrap\ActiveForm::begin([
'enableClientValidation' => false,
'options' => [
'enctype' => 'multipart/form-data',
],
]);
File should be uploading fine.
You can get uploaded file url using model call:
echo $model->getUploadedFileUrl('fileUpload');
ImageUploadBehavior
Image upload behavior extends file upload behavior with image thumbnails generation. You can configure set of different thumbnail profiles to generate.
Usage
Attach the behavior to your model class:
public function behaviors()
{
return [
[
'class' => '\bajadev\upload\ImageUploadBehavior',
'attribute' => 'imageUpload',
'thumbs' => [
'thumb' => ['width' => 400, 'height' => 300, 'crop' => true, 'quality' => 80, 'inset' => true],
],
'deleteOriginalFile' => true,
'rotateImageByExif' => true,
'filePath' => '@webroot/images/[[pk]].[[extension]]',
'fileUrl' => '@web/images/[[pk]].[[extension]]',
'thumbPath' => '@webroot/images/[[profile]]_[[pk]].[[extension]]',
'thumbUrl' => '@web/images/[[profile]]_[[pk]].[[extension]]',
],
];
}
You can use Yii path aliases
like @app, @webroot, @web in your path template configuration.
You can also use additional placeholders in path templates.
Placeholder [[foobar]] will be replaced with appropriate placeholder value.
Possible path/url placeholders:
[[model]]- model class name[[pk]]- value of the primary key[[id]]- the same as[[pk]][[attribute_name]]- attribute value, for example[[attribute_ownerId]][[slug_attribute_name]]- generate attribute value by Inflector, for example[[attribute_ownerId]][[md5_attribute_name]]- generate md5 value, for example[[attribute_ownerId]][[id_path]]- id subdirectories structure (if model primary key is12345, placeholder value will be1/2/3/4/5/[[basename]]- original filename with extension[[filename]]- original filename without extension[[extension]]- original extension[[profile]]- thumbnail profile name, use it in thumbnail path/url
Add validation rule:
public function rules()
{
return [
['imageUpload', 'file', 'extensions' => 'jpeg, gif, png'],
];
}
Setup proper form enctype:
$form = \yii\bootstrap\ActiveForm::begin([
'options' => [
'enctype' => 'multipart/form-data',
],
]);
File should be uploading fine.
You can get uploaded image url using model call:
echo $model->getImageFileUrl('imageUpload');
You can specify default image for models without uploaded image:
echo $model->getImageFileUrl('imageUpload', '/images/empty.jpg');
You can also get generated thumbnail image url:
echo $model->getThumbFileUrl('imageUpload', 'thumb');
You can specify default thumbnail image for models without uploaded image:
echo $model->getThumbFileUrl('imageUpload', 'thumb', '/images/thumb_empty.jpg');
Licence
MIT
bajadev/yii2-upload-behavior 适用场景与选型建议
bajadev/yii2-upload-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.09k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「file」 「upload」 「Behavior」 「resize」 「thumb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bajadev/yii2-upload-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bajadev/yii2-upload-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bajadev/yii2-upload-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
The Yii2 extension uses jQuery PrettyPhoto and OwlCarousel js and makes image galary from php array of structure defined.
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
统计信息
- 总下载量: 3.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-24