dbfernandes/yii2-cropper-cb

Yii2 Bootstrap Cropper Input (forked from bilginnet/yii2-cropper)

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 27

Language:JavaScript

Type:yii2-extension

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

This package is auto-updated.

Last update: 2024-10-20 03:20:23 UTC


README

Forked from Ercan Bilgin (bilginnet@gmail.com). Original repository: bilginnet/yii2-cropper.

CB.

Features

  • Crop
  • Image Rotate
  • Image Flip
  • Image Zoom
  • Coordinates
  • Image Sizes Info
  • Base64 Data

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Usage

Let's add into config in your main-local config file before return[]

       $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 [
           ....
       ]

Let's add in your model file

    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);
    }

Advanced usage in _form file

 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',

 ]);

Simple usage in _form file

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

Notes

Don't forget to add this line into root in .htaccess file

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

I will add jsOptions[] soon