Image Ajax
Image Ajax

This is the ImageAjax widget and a Yii 2 enhanced wrapper for the Dropzone library. A simple way to do ajax loading image on the site.

Installation
The preferred way to install this extension is through composer.

Either add

    "require": {
    "keygenqt/yii2-image-ajax": "*"
}
of your composer.json file.
Example form
Choose
Delete
This video will change its size to 350х350, so keep that in mind.
Example form code
    <?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'image')->widget(ImageAjax::class, [
    'label' => false,
    'btnSelect' => 'Choose',
    'btnDelete' => 'Delete',
    'url' => ['ajax/upload-image', 'type' => AjaxController::IMAGE_TEST],
    'subtitle' => 'This video will change its size to 350х350, so keep that in mind.',
    'afterUpdate' => 'function() {
        console.log("call afterUpdate")
    }'
]) ?>

<?= $form->field($model, 'integer')->textInput()->input('text', ['placeholder' => 'Integer']) ?>

<?= $form->field($model, 'email')->textInput()->input('email', ['placeholder' => 'Email']) ?>

<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>

<?php ActiveForm::end(); ?>
Example AjaxController
In the example was used ImageHandler You can get ImageHandler for composer & yii2 here: GitHub

    class AjaxController extends FrontendController
{
    const IMAGE_TEST = 'test';

    public function actionUploadImage($type)
    {
        if (Yii::$app->request->isAjax) {
            $url = self::uploadFile($type, 'file');
            if ($url) {
                echo Json::encode([
                    'url' => $url,
                    'error' => false,
                ]);
            } else {
                echo Json::encode([
                    'error' => 'Error upload file.',
                ]);
            }
            exit;
        }
    }

    public static function uploadFile($type, $name)
    {
        $file = UploadedFile::getInstanceByName($name);

        if (!empty($file)) {
            if (
                strpos($file->extension, 'png') !== false ||
                strpos($file->extension, 'jpg') !== false ||
                strpos($file->extension, 'jpeg') !== false
            ) {

                $name = uniqid();

                /** @var ImageHandler $imageHandler */
                $imageHandler = \Yii::$app->get('ih');

                $filePath =
                    Yii::getAlias('@app/../frontend/web/images/' . $type . '/') . $name . '.jpg';

                try {
                    switch ($type) {
                        case self::IMAGE_TEST:
                            $imageHandler->load($file->tempName)
                                ->adaptiveThumb(350, 350)
                                ->save($filePath, ImageHandler::IMG_JPEG);
                            break;
                    }
                    return "/images/$type/$name.jpg";

                } catch (Exception $e) {
                    return false;
                }
            }
        }
        return false;
    }
}
May 26, 2020