components/behaviors/ImageBehavior.php
public function uploadImage($event)
    {
        if (!array_key_exists($this->attribute, $event->sender->attributes)) {
            throw new InvalidConfigException(Yii::t('wavecms/main', 'Attribute {attribute} not found in model {model}', ['attribute' => $this->attribute, 'model' => $event->sender->className()]));
        }

        $oldFile = $event->sender->getOldAttribute($this->attribute);

        $uploadedFile = UploadedFile::getInstance($event->sender, $this->attribute);

        if (null !== $uploadedFile && $uploadedFile->size !== 0) {

            $folder = $this->getWebrootFolder();

            if ($oldFile) {
                $this->unlinkFiles($oldFile);
            }

            $baseName = $uploadedFile->baseName;
            $fileName = $baseName . '.' . $uploadedFile->extension;

            while (@file_exists($folder . '/' . $fileName)) {
                $baseName .= '_';
                $fileName = $baseName . '.' . $uploadedFile->extension;
            }

            FileHelper::createDirectory($folder, 0777);
            $uploadedFile->saveAs($folder . '/' . $fileName);

            FileHelper::createDirectory($folder . '/' . $this->thumbFolder, 0777);
            Image::thumbnail($folder . '/' . $fileName, $this->thumbWidth, $this->thumbHeight)
                ->save($folder . '/' . $this->thumbFolder . '/' . $fileName, ['quality' => $this->quality]);

            if (is_array($this->sizes)) {
                $i = 0;
                foreach ($this->sizes as $size) {
                    $manipulatorInterface = ManipulatorInterface::THUMBNAIL_OUTBOUND;

                    if (isset($size[2]) && in_array($size[2], [ManipulatorInterface::THUMBNAIL_OUTBOUND, ManipulatorInterface::THUMBNAIL_INSET], true)) {
                        $manipulatorInterface = $size[2];
                    }

                    FileHelper::createDirectory($folder . '/' . $i, 0777);
                    Image::thumbnail($folder . '/' . $fileName, $size[0], $size[1], $manipulatorInterface)
                        ->save($folder . '/' . $i . '/' . $fileName, ['quality' => $this->quality]);
                    $i++;
                }
            }

            $event->sender->{$this->attribute} = $fileName;
        } else {
            if (Yii::$app->request->post($this->attribute . '_image_delete')) {
                $this->unlinkFiles($oldFile);
                $event->sender->{$this->attribute} = null;
            } else {
                $event->sender->{$this->attribute} = $oldFile;
            }
        }
    }
public function uploadImage($event)
    {
        if (!array_key_exists($this->attribute, $event->sender->attributes)) {
            throw new InvalidConfigException(Yii::t('wavecms/main', 'Attribute {attribute} not found in model {model}', ['attribute' => $this->attribute, 'model' => $event->sender->className()]));
        }

        $oldFile = $event->sender->getOldAttribute($this->attribute);

        $uploadedFile = UploadedFile::getInstance($event->sender, $this->attribute);

        if (null !== $uploadedFile && $uploadedFile->size !== 0) {

            $folder = $this->getWebrootFolder();

            if ($oldFile) {
                $this->unlinkFiles($oldFile);
            }

            $baseName = $uploadedFile->baseName;
            $fileName = $baseName . '.' . $uploadedFile->extension;

            while (@file_exists($folder . '/' . $fileName)) {
                $baseName .= '_';
                $fileName = $baseName . '.' . $uploadedFile->extension;
            }

            FileHelper::createDirectory($folder, 0777);
            $uploadedFile->saveAs($folder . '/' . $fileName);

            FileHelper::createDirectory($folder . '/' . $this->thumbFolder, 0777);
            Image::thumbnail($folder . '/' . $fileName, $this->thumbWidth, $this->thumbHeight)
                ->save($folder . '/' . $this->thumbFolder . '/' . $fileName, ['quality' => $this->quality]);

            if (is_array($this->sizes)) {
                $i = 0;
                foreach ($this->sizes as $size) {
                    $manipulatorInterface = ManipulatorInterface::THUMBNAIL_OUTBOUND;

                    if (isset($size[2]) && in_array($size[2], [ManipulatorInterface::THUMBNAIL_OUTBOUND, ManipulatorInterface::THUMBNAIL_INSET], true)) {
                        $manipulatorInterface = $size[2];
                    }

                    FileHelper::createDirectory($folder . '/' . $i, 0777);
                    Image::thumbnail($folder . '/' . $fileName, $size[0], $size[1], $manipulatorInterface)
                        ->save($folder . '/' . $i . '/' . $fileName, ['quality' => $this->quality]);
                    $i++;
                }
            }

            $event->sender->{$this->attribute} = $fileName;
        } else {
            if (Yii::$app->request->post($this->attribute . '_image_delete')) {
                $this->unlinkFiles($oldFile);
                $event->sender->{$this->attribute} = null;
            } else {
                $event->sender->{$this->attribute} = $oldFile;
            }
        }
    }
Upload images on save and update event
@param $event
@throws InvalidConfigException
@throws \yii\base\Exception