定制 lyhoshva/yii2-cover-behavior 二次开发

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

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

lyhoshva/yii2-cover-behavior

Composer 安装命令:

composer require lyhoshva/yii2-cover-behavior

包简介

Yii 2 behavior for image uploading

README 文档

README

Yii 2 behavior for image uploading

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist lyhoshva/yii2-cover-behavior

or add

"lyhoshva/yii2-cover-behavior": "*"

to the require section of your composer.json.

Basic Usage

This extension provides support for ActiveRecord image uploading. This support is granted via [[\lyhoshva\Cover\CoverBehavior]] ActiveRecord behavior. You'll need to attach it to your ActiveRecord class and point next options for it:

class Article extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'image' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'image_name', // attribute, which will contains image_name
                // virtual attribute, which is used for image uploading ("image" by default)
                'relationReferenceAttribute' => 'image', 
                'path' => 'uploads/articles/', // path to upload directory 
            ],
        ];
    }

    public function rules()
    {
        return [
            [['image'], 'required', 'on' => 'create'],
            [['image', 'cover_image'], 'file', 'extensions' => ['png', 'jpg', 'jpeg', 'gif']],
        ];
    }
}

Attention: do NOT declare relationReferenceAttribute attribute in the owner ActiveRecord class. Make sure it does not conflict with any existing owner field or virtual property.

Virtual property declared via relationReferenceAttribute serves not only for saving. It also contains existing references for the uploaded image

Advanced usage

Behavior can generate path ('path') or image name ('fileNameGenerator') automatically by writing generation rule using callback function. Or you can set static path or image name specifying String values. Callback function get ActiveRecord model object as argument.

You can specify image path ('modelAttributeFilePath') model attribute to save generated path in Data Base. It use only path configured as callback function. If this option not set and path is callback, image path will be saved to 'modelAttribute' as image path + image name string.

public function behaviors()
    {
        'cover' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'cover_name',
                
                'relationAttribute' => 'cover',
                // not required options
                'path' => function ($model) {
                    return 'web/uploads/' . Inflector::slug($model->name) . '/'; 
                    // generated path should have forward slash at the end;
                },
                'modelAttributeFilePath' => 'cover_path',
                'fileNameGenerator' => function ($model) {
                    return Inflector::slug($model->name);
                },
            ],
    }

Thumbnails

If you want to use thumbnails:

public function behaviors()
    {
        return [
            'image' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'image_name', // attribute, which will be handled
                // virtual attribute, which is used for image uploading ("image" by default)
                'relationReferenceAttribute' => 'image', 
                'path' => 'uploads/articles/', // path to upload directory 
                'thumbnails' => [
                    [
                        'prefix' => 'thumb_', //image prefix for thumbnail
                        'width' => 220, // max width
                        'height' => 160, // max height
                        'mode' => CoverBehavior::THUMBNAIL_INSET, 
                    ]
                ],
            ],
        ];
    }

To see information about mods read [Imagine] (https://imagine.readthedocs.io/)

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固