PHP code example of ereminmdev / yii2-cropperimageupload

1. Go to this page and download the library: Download ereminmdev/yii2-cropperimageupload 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/ */

    

ereminmdev / yii2-cropperimageupload example snippets


public function behaviors()
{
    return [
        'avatar' => [
            'class' => CropperImageUploadBehavior::class,
            'attribute' => 'avatar',
            'scenarios' => ['default', 'create', 'update'],
            'placeholder' => '@app/modules/user/assets/images/no-avatar.jpg',
            'path' => '@webroot/upload/avatar/{id}',
            'url' => '@web/upload/avatar/{id}',
            'thumbs' => [
                'thumb' => ['width' => 60, 'height' => 60, 'quality' => 80, 'mode' => ManipulatorInterface::THUMBNAIL_OUTBOUND],
                'preview' => ['width' => 240, 'height' => 240, 'bg_alpha' => 0],
            ],
            'cropAspectRatio' => 1,
        ],
    ];
}

public function rules()
{
    return [
        [['avatar'], 'file', 'extensions' => 'jpg, jpeg, gif, png, svg, webp'],
    ];
}

<?= $form->field($model, 'avatar')->widget(CropperImageUploadWidget::class) 

<?= $model->renderThumbImage('avatar') 

foreach (User::find()->each() as $model) {
    $filename = $model->getAttribute('avatar');

    if ($model->recreateThumbs('avatar', true, true)) {
        $this->stdout('Recreated successful: ' . $filename . "\n");
    } else {
        $this->stdout('Error when recreating: ' . $filename . "\n", Console::FG_RED);
    }
}