承接 salopot/yii2-attach-image 相关项目开发

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

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

salopot/yii2-attach-image

Composer 安装命令:

composer require salopot/yii2-attach-image

包简介

Provides behaviors for attach files to model and manipulate it if files is image

README 文档

README

Provides behaviors for attach files to model and manipulate it if files is image

Features

  • Multilevel file store structure
  • Can generate thumbnail manual, by demand, after upload or return image as base64 encoded string
  • Automatic delete linked file when delete model
  • Allow use any image manipulation component. Default use imagine

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist salopot/yii2-attach-image "dev-master"

or add

"salopot/yii2-attach-image": "dev-master"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

//in model

use salopot\attach\behaviors\AttachFileBehavior;
use salopot\attach\behaviors\AttachImageBehavior;
...

public function rules()
{
    return [
        ...
        [['origin_name'], 'string', 'max' => 255],
        [['image'], 'image'],
    ];
}

public function behaviors()
{
    return [
        'image' => [
            'class' => AttachImageBehavior::className(),
            'attributeName' => 'image',
            //'relativeTypeDir' => '/upload/images/test/',
            'types' => array(
                'thumb' => array(
                    //'format' => 'gif', //"gif", "jpeg", "png", "wbmp", "xbm"
                    'process' => function($behavior, $image) {
                        return $image->thumbnail(new \Imagine\Image\Box(150, 150));
                    }
                ),
                'background' => array(
                    'process' => function($behavior, $image) {
                        $image = $image->thumbnail(new \Imagine\Image\Box(150, 150));
                        $image->effects()->grayscale();
                        return $image;
                    },
                ),
                'main' => array(
                    //'processOn' => AttachImageBehavior::PT_DEMAND, //PT_RENDER, PT_BASE64_ENCODED,
                    'process' => function($behavior, $image) {
                        $watermark = \yii\imagine\Image::getImagine()->open(Yii::$app->params['watermark']);
                        $size = $image->getSize();
                        $wSize = $watermark->getSize();
                        $bottomRight = new \Imagine\Image\Point($size->getWidth() - $wSize->getWidth(), $size->getHeight() - $wSize->getHeight());
                        $image->paste($watermark, $bottomRight);
                        $image = $image->thumbnail(new \Imagine\Image\Box(150, 150));
                        return $image;
                    },
                ),
            ),
        ]
    ];
}

//Also can store extended info

public function init()
{
    parent::init();
    $this->on(AttachFileBehavior::EVENT_AFTER_ATTACH_DATA, [$this, 'afterAttachData']);
}

public function afterAttachData($event) {
    $this->origin_name = $event->uploadedFile->name;
}

//in form edit view:
<?= $form->field($model, 'image')->fileInput() ?>

//in view:
<?= Html::img($model->getBehavior('image')->getUrl('thumb')); ?>
<?= Html::img($model->getBehavior('image')->getUrl('background')); ?>
<?= Html::img($model->getBehavior('image')->getUrl('main')); ?>

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-22