简体   繁体   中英

Display images with different extensions Yii2

Please, help me to display images with different extensions.

Here is my code to display

<?php
        foreach ($users as $arr) {
            $imageName = $arr->username;
            $extension = 'jpg';
        ?>
        <div class="col-sm-6 col-md-4">
            <div class="thumbnail">
                <?= Html::img('/uploads'.'/'.$imageName.'.'.$extension, ['class' => 'img-rounded', 'style' => 'width: 300px; height: 250px'])   ?>

This code displaying only 'jpg' images, and i need to display for e. 'jpg','png'.

or You can check file exists

$extensions = ['jpg','png','gif'];
foreach($extensions as $extension){
    if(file_exists(Yii::getAlias('@web').'path/to/uploads'.$imageName.'.'.$extension)) {
        echo Html::img('/uploads/'.$imageName.'.'.$extension, ['class' => 'img-rounded', 'style' => 'width: 300px; height: 250px']);
        break;
    }
}

but it's wrong way you should do this as Insane Skull said as

you can store image name with extension, so no need of this kind of stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM