PHP code example of crazysnowflake / yii2-image-cropper
1. Go to this page and download the library: Download crazysnowflake/yii2-image-cropper 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/ */
crazysnowflake / yii2-image-cropper example snippets
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' ] );
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);
}
}