Yii2 Render an image in DetailView

2019-03-13 02:19发布

问题:

I am trying to render an Image in view file. My code is like below:

<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'name',
            //'photo',
            [
                'attribute'=>'photo',
                'value'=>('<img src =' .'uploads/' . $model->photo . ' height="100" width="100"' .   '>')
            ],


            [
                'attribute' => 'birth_date',
                'format' => ['date', 'dd-MM-Y'],
                        ],  
        'mobile',
            ],

    ]) ?>

Though the code below works:

<?php echo ('<img src =' .'uploads/' . $model->photo .'>'); ?>

Thanks.

回答1:

Try this:

[
    'attribute'=>'photo',
    'value'=>$model->photo,
    'format' => ['image',['width'=>'100','height'=>'100']],
],


回答2:

 DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'photo:image',
        ],
    ])


回答3:

Try this:

[
   'attribute'=>'images',
   'value'=> Yii::$app->homeUrl.'uploads/'.$model->images,
   'format' => ['image',['width'=>'100','height'=>'100']],
],


回答4:

[
 'attribute'=>'group_pic',
'value'=>('uploads/group_pro_pic/' . $model->group_pic),
'format' => ['image',['width'=>'230','height'=>'200']],
            ]


标签: yii2