dev-master
9999999-devYii2 Bootstrap Cropper Input (forked from bilginnet/yii2-cropper)
MIT
The Requires
extension yii2 widget cropper
Yii2 Bootstrap Cropper Input (forked from bilginnet/yii2-cropper)
Forked from Ercan Bilgin (bilginnet@gmail.com). Original repository: bilginnet/yii2-cropper., (*1)
CB., (*2)
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist dbfernandes/yii2-cropper-cb "dev-master"
or add, (*5)
"dbfernandes/yii2-cropper-cb": "dev-master"
to the require section of your composer.json
file., (*6)
Let's add into config in your main-local config file before return[], (*7)
$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, (*8)
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); }
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', ]);
echo $form->field($model, '_image')->widget(\dbfernandes\cropper\Cropper::className(), [ 'cropperOptions' => [ 'width' => 100, // must be specified 'height' => 100, // must be specified ] ]);
Don't forget to add this line into root in .htaccess file, (*9)
RewriteRule ^uploads/(.*)$ uploads/$1 [L]
Yii2 Bootstrap Cropper Input (forked from bilginnet/yii2-cropper)
MIT
extension yii2 widget cropper