dbfernandes/yii2-cropper-cb

Yii2 Bootstrap Cropper 输入 (从 bilginnet/yii2-cropper 分支)

安装: 21

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 27

语言:JavaScript

类型:yii2-extension

dev-master 2017-11-15 10:23 UTC

This package is auto-updated.

Last update: 2024-09-20 03:14:47 UTC


README

从 Ercan Bilgin (bilginnet@gmail.com) 分支。原始仓库: bilginnet/yii2-cropper。

CB.

功能

  • 裁剪
  • 图片旋转
  • 图片翻转
  • 图片缩放
  • 坐标
  • 图片尺寸信息
  • Base64 数据

安装

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

运行以下命令之一:

php composer.phar require --prefer-dist dbfernandes/yii2-cropper-cb "dev-master"

"dbfernandes/yii2-cropper-cb": "dev-master"

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

用法

在返回 [] 之前,将以下内容添加到您的 main-local 配置文件中

       $baseUrl = str_replace('/backend/web', '', (new Request)->getBaseUrl());
       $baseUrl = str_replace('/frontend/web', '', $baseUrl);

       Yii::setAlias('@uploadUrl', $baseUrl.'/uploads/');
       Yii::setAlias('@uploadPath', realpath(dirname(__FILE__).'/../../uploads/'));
       // image file will upload in //root/uploads   folder

       return [
           ....
       ]

在您的模型文件中添加以下内容

    public $_image

    public function rules()
    {
        return [
            ['_image', 'safe'],
        ];
    }

    public function beforeSave($insert)
    {
        if (is_string($this->_image) && strstr($this->_image, 'data:image')) {

            // creating image file as png
            $data = $this->_image;
            $data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
            $fileName = time() . '-' . rand(100000, 999999) . '.png';
            file_put_contents(Yii::getAlias('@uploadPath') . '/' . $fileName, $data);


            // deleting old image
            // $this->image is real attribute for filename in table
            // customize your code for your attribute            
            if (!$this->isNewRecord && !empty($this->image)) {
                unlink(Yii::getAlias('@uploadPath/'.$this->image));
            }

            // set new filename
            $this->image = $fileName;
        }

        return parent::beforeSave($insert);
    }

_form 文件中的高级用法

 echo $form->field($model, '_image')->widget(\dbfernandes\cropper\Cropper::className(), [
    'cropperOptions' => [
        'width' => 100, // must be specified
        'height' => 100, // must be specified

        // optional
        // url must be set in update action
        'preview' => [
            'url' => '', // set in update action // (!$model->isNewRecord) ? Yii::getAlias('@uploadUrl/$model->image') : ''
            'width' => 100, // default 100 // default is cropperWidth if cropperWidth < 100
            'height' => 100, // Will calculate automatically by aspect ratio if not set
        ],

        // optional // defaults following code
        // you can customize
        'icons' => [
            'browse' => '<i class="fa fa-image"></i>',
            'crop' => '<i class="fa fa-crop"></i>',
            'close' => '<i class="fa fa-crop"></i>',
        ]
    ],

    // optional // defaults following code
    // you can customize
    'label' => '$model->attribute->label',

 ]);

_form 文件中的简单用法

 echo $form->field($model, '_image')->widget(\dbfernandes\cropper\Cropper::className(), [
    'cropperOptions' => [
        'width' => 100, // must be specified
        'height' => 100, // must be specified
     ]
]);

注意

不要忘记在 .htaccess 文件的根目录中添加此行

RewriteRule ^uploads/(.*)$ uploads/$1 [L]

我很快会添加 jsOptions[]