Saturday, June 11, 2016

Yii2 Show image at a GridView

[
                'attribute' => 'itm_image',
                'format' => 'image',
                'value' => function($data) {
                    if($data->itm_image){
                        return \Yii::getAlias('@web/uploads/products/').$data->itm_image;
                    }
                    else{
                        return \Yii::getAlias('@web/uploads/products/no.png');
                    }
                },
                'contentOptions' => ['class' => 'item_image_grid img-responsive img-rounded']
            ],
            [
                'attribute' => 'itm_image',
                'format' => 'html',
                'value' => function ($data) {
                    if($data->itm_image) {
                        return Html::img('@web/uploads/products/' . $data->itm_image,
                            ['width' => '60px', 'class' => 'img-responsive img-rounded']);
                    }
                    else{
                        return Html::img('@web/uploads/products/no.png',
                            ['width' => '60px', 'class' => 'img-responsive img-rounded']);
                    }
                },
            ],

Sunday, June 5, 2016

yii2 file upload


if ($model->load(Yii::$app->request->post())){
            $image = UploadedFile::getInstance($model, 'image_file');
            $fileext=time();
            $image->saveAs('uploads/' . $fileext.$image->baseName . '.' . $image->extension);
            $model->image_file = $fileext.$image->baseName.'.'.$image->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }
        else{
            return $this->render('create', [
                'model' => $model,
            ]);
        }