Set width image in gridview yii2

16,209

Solution 1

You can define class for cells as in example

[
    'attribute' => 'title',
    'format' => 'image',
    'value' => function($data) { return 'you_image.jpeg'; },
    'contentOptions' => ['class' => 'come-class']
],

Then set image width using css. If image width can be different, use html format

[
    'attribute' => 'title',
    'format' => 'html',
    'value' => function($data) { return Html::img('you_image.jpeg', ['width'=>'100']); },
],

Solution 2

Use:

'value' => function($data) {
        return Html::img($data->imageurl,['width'=>100]);
   },

And in your model:

  /*
    Return Image url path
  */
   public function getimageurl()
   {
      // return your image url here
      return \Yii::$app->request->BaseUrl.'/uploads/'.$this->ImagePath;
   }

Solution 3

Use

'format' => ['image',['width'=>'100','height'=>'100']],

example:

[           
      'attribute' => 'image',
      'format' => ['image',['width'=>'100','height'=>'100']],
      'value' => function($dataProvider) { return $dataProvide->image; },


],
Share:
16,209
Aditya Dharma
Author by

Aditya Dharma

Updated on June 25, 2022

Comments

  • Aditya Dharma
    Aditya Dharma almost 2 years

    how to set width image in gridview widget in yii2?

    I've been using this method to display the image. Image in gridview in yii2

  • kaynewilder
    kaynewilder about 9 years
    @kopletusprime @Alex doesn't work for me. I have 'value'=>function($data) { return $data->imageurl; }, which is NOT a static image URL. so where do I put ['width'=>'100']??? I tried everything where I think it fits but I got errors