Show image at a GridView from URL in a database

I would like to know how I can show images (stored in a folder of my web-project) via a GridView in Yii2. The images are associated to an URL which is stored in a database …

  • MySQL -> Platform table: ID | Name | Img (Image’s URL)

  • View -> GridView which should show image from Image’s URL on MySQL


<?php


use yii\helpers\Html;

use yii\grid\GridView;


/* @var $this yii\web\View */

/* @var $dataProvider yii\data\ActiveDataProvider */


$this->title = 'Platforms';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="platform-index">


    <h1><?= Html::encode($this->title) ?></h1>


    <p>

        <?= Html::a('Create Platform', ['create'], ['class' => 'btn btn-success']) ?>

    </p>


    

       <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [

            	

            'id',

            'name', 

            'img',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

    

</div>




<?= GridView::widget([

    'dataProvider' => $dataProvider,

    'columns' => [


        'id',

        'name',

        [

            'attribute' => 'img',

            'format' => 'html',

            'label' => 'ImageColumnLable',

            'value' => function ($data) {

                return Html::img('/pathToImage/' . $data['img'],

                    ['width' => '60px']);

            },

        ],

        ['class' => 'yii\grid\ActionColumn'],

    ],

]);

?>



Thanks a lot korshak! It works perfectly!

Hello. I am trying to use this code but I get error: Class ‘HTMLPurifier_Config’ not found.

The folder "vendor/ezyang/htmlpurifier" is present.

As a workaround I changed ‘format’ from ‘html’ to ‘raw’.