疯狂雪花/yii2-image-cropper

JavaScript 图像裁剪器

安装: 13

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

类型:yii2-extension

1.0.1 2020-05-22 14:11 UTC

This package is auto-updated.

Last update: 2024-09-22 23:36:01 UTC


README

安装

安装此扩展的首选方式是通过 composer。请查看此扩展的依赖和要求,见 composer.json。阅读关于设置应用程序 composer.json 的 minimum-stability 设置的 网络提示 /wiki

安装方法如下:

$ php composer.phar require crazysnowflake/yii2-image-cropper "^1.0@dev"

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

"crazysnowflake/yii2-image-cropper": "^1.0@dev"

to the require section of your composer.json file.

版本变更

注意:有关各个版本变更的详细信息,请参阅 变更日志

用法

use crazysnowflake\imagecropper\ImageCropperWidget;

$form = ActiveForm::begin( [
        'id'                   => 'edit-profile',
        'options'              => [ 'enctype' => 'multipart/form-data' ],
        'fieldConfig'          => [
            'inputOptions' => [ 'class' => 'form-control form-control-lg' ]
        ],
    ] );
    
echo $form->field( $model, 'virtualImage' )
          ->widget( ImageCropperWidget::classname(), [
                'options'       => [
                    'id'       => 'user-avatar',
                    'accept'   => 'image/*',
                    'multiple' => false,
                ],
                'templateImage' => $model->avatar ? \yii\helpers\Html::img( $model->avatar ) : null,
            ] )->label( 'Profile Image' )->hint( 'We recommend a square image for best results. The ideal size would be 200px wide by 200px high.', [ 'class' => 'hint-block text-muted' ] ); ?>

ActiveForm::end();

#####您的控制器函数

use crazysnowflake\imagecropper\ImageCropper;

 
protected function saveImageProfile() {
    $this->virtualImageProfile = UploadedFile::getInstance($this, 'virtualImageProfile');
    
    if ($this->virtualImageProfile && $this->virtualImageProfile->type === 'image/jpeg') {    
        $data = Yii::$app->request->post( 'cropdata' );
        if( $data && isset($data['virtualImageProfile']) && $data['virtualImageProfile'] ){
            ImageCropper::cropImageSection($this->virtualImageProfile->tempName, $this->virtualImageProfile->tempName, $data['virtualImageProfile']);
        }
        $filename = uniqid() . '.' . $this->virtualImageProfile->extension;
        $this->virtualImageProfile->saveAs(Yii::getAlias('@app') . "/web/uploads/users/" . $filename);
    
    }
}

许可证

yii2-image-cropper 采用 MIT 许可证发布。有关详细信息,请参阅附带 LICENSE.md 文件。