Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 how to display images saved in web folder

Tags:

image

yii2

I have images saved in /web/uploads/PROJECT in Yii2. How do i display them on page? below is my codes but its broken link image.

Controller:

$img = Yii::$app->request->baseUrl.'/web/uploads/PROJECT/';
$image = Html::img($img.$img_obj['AVATAR'],["width"=>"600px"]);

View:

echo $data;

Do i have to set any rights to display it? Right-clicking and opening image in new tab shows the url "/web/uploads/PROJECT/3fioobapJ5vRk_wdCEzDJbQWyO66inWO.jpg" which seems to be correct but page displays "Not Found (#404)"

Can anyone kindly advise? Thanks!

like image 408
esiaz Avatar asked Feb 04 '15 06:02

esiaz


People also ask

How to display image from database in Yii2?

You can change width as needed. //'yourimage', <--Replace this line with array as below ['label' => 'Your Image', 'format' => ['image',['width'=>'50']], 'value'=>function($model){ return('@web/images/'. $model->yourimage); }, ], You can change width as needed.


3 Answers

Strangely, using the below codes displays the images correctly.

$img = Url::to('@web/uploads/PROJECT/').$img_obj['AVATAR'];                 
$image = '<img src="'.$img.'" width="600" />';  


 <img src="<?= Yii::$app->request->baseUrl . '/backend/web/uploads/' . $model->profile_photo ?>" class=" img-responsive" >  
<?php echo Html::img('@web/img/icon.png', ['class' => 'pull-left img-responsive']); ?>
like image 86
esiaz Avatar answered Oct 01 '22 03:10

esiaz


The static images should be placed inside web/any_folder_name

to access in YII 2:

<?= Html::img('@web/any_folder_name/bg1.jpg', ['alt'=>'some', 'class'=>'thing']);?>
like image 28
iltaf khalid Avatar answered Oct 01 '22 03:10

iltaf khalid


Add this code in your Grid View:

        [
            'attribute'=>'photo',
            'value' =>  Html::a(Html::img(Yii::getAlias('@web').'/image/'.$st_data->photo, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']), ['site/zoom']),
            'format' => ['raw'],
        ],
like image 23
vishuB Avatar answered Oct 01 '22 03:10

vishuB