maxmirazh33 / yii2-uploadable-cropable-image

用于上传和裁剪图片的Yii2扩展

v2.2.5 2020-02-19 06:02 UTC

This package is auto-updated.

Last update: 2024-08-27 19:48:55 UTC


README

用于上传和裁剪图片的Yii2扩展

Latest Version Software License Quality Score Total Downloads

安装

安装此扩展的首选方式是通过Composer

运行以下命令之一:

php composer.phar require --prefer-dist maxmirazh33/yii2-uploadable-cropable-image "*"

"maxmirazh33/yii2-uploadable-cropable-image": "*"

将以下内容添加到您的composer.json文件的require部分:

使用方法

扩展安装完成后,只需在代码中按以下方式使用即可:

在您的模型中

public function behaviors()
{
    return [
        [
            'class' => \maxmirazh33\image\Behavior::className(),
            'savePathAlias' => '@web/images/',
            'urlPrefix' => '/images/',
            'crop' => true,
            'attributes' => [
                'avatar' => [
                    'savePathAlias' => '@web/images/avatars/',
                    'urlPrefix' => '/images/avatars/',
                    'width' => 100,
                    'height' => 100,
                ],
                'logo' => [
                    'crop' => false,
                    'thumbnails' => [
                        'mini' => [
                            'width' => 50,
                        ],
                    ],
                ],
            ],
        ],
    //other behaviors
    ];
}

使用规则验证属性。

在您的视图文件中

echo $form->field($model, 'avatar')->widget('maxmirazh33\image\Widget');

然后,在您的视图中

echo Html::img($model->getImageUrl('avatar'));
echo Html::img($model->getImageUrl('logo', 'mini')); //get url of thumbnail named 'mini' for 'logo' attribute

如果您使用高级应用程序模板并且该行为附加在后台模型中,那么在前端模型中添加特性

use \maxmirazh33\image\GetImageUrlTrait

并且为前端模型也使用getImageUrl()方法。