1. Go to this page and download the library: Download softark/yii2-cropper-bs5 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
softark / yii2-cropper-bs5 example snippets
$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
Yii::setAlias('@imagesUrl', $baseUrl . '/images/');
Yii::setAlias('@imagesPath', realpath(dirname(__FILE__) . '/../images/'));
// image file will be stored in //root/images 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, for example
// cropper sends image data in a base64 encoded string
$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('@imagesPath') . '/' . $fileName, $data);
// deleting old image file if any
// $this->filename is the real attribute for the filename
// customize your code for your attribute
if (!$this->isNewRecord && !empty($this->filename)) {
@unlink(Yii::getAlias('@imagesPath') . '/' . $this->filename);
}
// set new filename
$this->filename = $fileName;
}
return parent::beforeSave($insert);
}
echo $form->field($model, '_image')->widget(\softark\cropper\Cropper::class, [
// Unique ID of the cropper. Will be set automatically if not set.
'uniqueId' => 'image_cropper',
// The url of the initial image.
// You can set the current image url for update scenario, and
// set null for create scenario.
// Defaults to null.
'imageUrl' => ($model->isNewRecord) ? null : Yii::getAlias('@imagesUrl') . $model->filename,
// Cropper options
'cropperOptions' => [
// The dimensions of the image to be cropped and saved.
// You have to specify both width and height.
'width' => 1200,
'height' => 800,
// Preview window options
'preview' => [
// The dimensions of the preview image must be specified.
'width' => 600, // you can set as string '100%'
'height' => 400, // you can set as string '100px'
// The url of the initial image for the preview.
'url' => (!empty($model->filename)) ? Yii::getAlias('@imagesUrl' . '/' . $model->filename) : null,
],
// Whether to use FontAwesome icons
'useFontAwesome' => true, // default = false : use Unicode Chars
],
// Modal options
'modalOptions' => [
// Specify the size of the modal.
// 'modal-md', 'modal-lg', or 'modal-xl'
// Default and recommended value is 'modal-lg'
'modalClass' => 'modal-lg',
],
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.