ereminmdev / yii2-cropperimageupload
Image crop and upload for Yii framework.
Package info
github.com/ereminmdev/yii2-cropperimageupload
Type:yii2-extension
pkg:composer/ereminmdev/yii2-cropperimageupload
1.0.68
2025-05-29 07:35 UTC
Requires
- php: >=7.4
- mohorev/yii2-upload-behavior: ^0.2
- npm-asset/cropperjs: ^1.0
- yiisoft/yii2: ~2.0.1
- yiisoft/yii2-imagine: ^2.1
This package is auto-updated.
Last update: 2026-05-29 01:16:16 UTC
README
Image crop and upload for Yii framework.
Depends on:
Install
composer require --prefer-dist ereminmdev/yii-cropperimageupload
Use
Add some code to model and view files:
- model:
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:
<?= $form->field($model, 'avatar')->widget(CropperImageUploadWidget::class) ?>
- html image:
<?= $model->renderThumbImage('avatar') ?> <?= $model->renderThumbImage('avatar', 'preview', ['alt' => 'Avatar']) ?>
Tips
- Re-create thumbs:
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); } }