sergeykoz/yii2-imageupload

为 Yii2 优化的图像上传小部件,支持裁剪

安装数: 13,711

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 1

开放性问题: 0

类型:yii2-extension

0.3.1 2021-02-06 21:48 UTC

This package is not auto-updated.

Last update: 2024-09-27 07:10:28 UTC


README

安装

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

运行以下命令之一

php composer.phar require --prefer-dist sergeykoz/yii2-imageupload

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

"sergeykoz/yii2-imageupload": "0.3.1",

使用方法

将上传小部件添加到表单中

<?php
    use ssoft\imageupload\ImageUpload;    
    
    echo $form = ActiveForm::begin(['id' => 'image-form', 'options' => ['enctype'=>'multipart/form-data']]);
    
    // first way
    echo ImageUpload::widget([
        'model' => $photoModel,
        'attribute' => 'photo',
        'parametersAttibute' => 'photo_parameters',
        'addClass' => 'col-sm-8',
        'imageUrl' => Yii::getAlias('@web') . '/files',
        'imagePath' => Yii::getAlias('@webroot') . '/files',
        'placeholder' => 'Photo',
        'size' => ['height' => 300, 'width' => 100],    
        'aspectRatio' => 0.33,
        'disabled' => false
    ]);
        
    // second way
    echo $form->field($photoModel, 'image')->widget(ImageUpload::className(), [
        'parametersAttibute' => 'image_parameters',
        'imageUrl' => Yii::getAlias('@web') . '/files',
        'imagePath' => Yii::getAlias('@webroot') . '/files',
        'placeholder' => 'Image'
    ]);
    
    echo ActiveForm::end();
?>

配置 main.php

'controllerMap' => [            
    'imageupload' => 'ssoft\imageupload\ImageController',
],

管理上传的图像

<?php
    use ssoft\imageupload\Image;

    // create instance of the image
    $image = new Image([
        'imagePath' => Yii::getAlias('@webroot') . '/files',
        'imageFile' => $photoModel->photo,
        'parameters' => $photoModel->photo_parameters,
    ]);
    
    // save the image with size 100x300 with name Filename100x300.Ext
    $image->save(
        Yii::getAlias('@webroot') . '/files',
        Image::thumbnailName($photoModel->photo, ['height' => 300, 'width' => 100]), 
        ['height' => 300, 'width' => 100]
    );
    
    // get content of the image png
    echo $image->show('png', ['height' => 600, 'width' => 600]);
?>