elgorm/yii2-uploadable-cropable-image

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

该包的官方仓库似乎已消失,因此该包已被冻结。

安装: 132

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 10

类型:yii2-extension

v2.2.4 2015-10-20 07:25 UTC

This package is not auto-updated.

Last update: 2020-01-28 20:12:24 UTC


README

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

Latest Version Software License Quality Score Code Climate Total Downloads

安装

安装此扩展的首选方法是通过composer

运行以下命令之一:

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

或者

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

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

使用方法

扩展安装完成后,只需在代码中使用它即可

在你的模型中

public function behaviors()
{
    return [
        [
            'class' => \elgorm\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('elgorm\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 \elgorm\image\GetImageUrlTrait

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