zxbodya/yii2-image-attachment 问题修复 & 功能扩展

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

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

zxbodya/yii2-image-attachment

Composer 安装命令:

composer require zxbodya/yii2-image-attachment

包简介

Extension for yii, to handle images attached to ActiveRecord

README 文档

README

This extension intended to handle images associated with model.

Extensions provides user friendly widget, for image upload and removal.

Yii2 image attachement screenshot

Features

  1. Asynchronous image upload
  2. Ability to generate few image versions with different configurations
  3. Drag & Drop

Decencies

  1. Yii2
  2. Twitter bootstrap assets
  3. Imagine library

Installation:

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist zxbodya/yii2-image-attachment "*@dev"

or add

"zxbodya/yii2-image-attachment": "*@dev"

to the require section of your composer.json file.

Usage

Add ImageAttachmentBehavior to your model, and configure it, create folder for uploaded files.

public function behaviors()
{
    return [
        'coverBehavior' => [
            'class' => ImageAttachmentBehavior::className(),
            // type name for model
            'type' => 'post',
            // image dimmentions for preview in widget 
            'previewHeight' => 200,
            'previewWidth' => 300,
            // extension for images saving
            'extension' => 'jpg',
            // path to location where to save images
            'directory' => Yii::getAlias('@webroot') . '/images/post/cover',
            'url' => Yii::getAlias('@web') . '/images/post/cover',
            // additional image versions
            'versions' => [
                'small' => function ($img) {
                    /** @var ImageInterface $img */
                    return $img
                        ->copy()
                        ->resize($img->getSize()->widen(200));
                },
                'medium' => function ($img) {
                    /** @var ImageInterface $img */
                    $dstSize = $img->getSize();
                    $maxWidth = 800;
                    if ($dstSize->getWidth() > $maxWidth) {
                        $dstSize = $dstSize->widen($maxWidth);
                    }
                    return [
                        $img->copy()->resize($dstSize),
                        ['jpeg_quality' => 80], // options used when saving image (Imagine::save)
                    ];
                },
            ]
        ]
    ];
}

Add ImageAttachmentAction in controller somewhere in your application. Also on this step you can add some security checks for this action.

public function actions()
{
    return [
        'imgAttachApi' => [
            'class' => ImageAttachmentAction::className(),
            // mappings between type names and model classes (should be the same as in behaviour)
            'types' => [
                'post' => Post::className()
            ]
        ],
    ];
}

Add ImageAttachmentWidget somewhere in you application, for example in editing from.

echo ImageAttachmentWidget::widget(
    [
        'model' => $model,
        'behaviorName' => 'coverBehavior',
        'apiRoute' => 'test/imgAttachApi',
    ]
)

Done! Now, you can use it in other places in app too:

if ($model->getBehavior('coverBehavior')->hasImage()) {
    echo Html::img($model->getBehavior('coverBehavior')->getUrl('medium'));
}

统计信息

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

GitHub 信息

  • Stars: 34
  • Watchers: 5
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-02