douliu8327 2015-08-20 15:20
浏览 97
已采纳

使用PHP Glob显示图像时添加alt,标题和格式

So I am using this bit of PHP to display all the images in a particular directory:

<?php
  $dirname = "cards/";
  $images = glob($dirname."*.png");
  foreach($images as $image) {
  echo '<img src="'.$image.'" />';
  }
?>

It works fine to load my images. But I am wondering how I can have it echo the html to not only load the image, but display the file name as alt and title attributes. I tried using the following but it loads up the dir name as well as the file name. Trying to just get the file name.

echo '<img src="'.$image.'" alt="'.$image.'" title="'.$image.'" />';

In addition, I want to display only five images per row, is there a way of formatting that with automatically generated images?

UPDATE: In regards to displaying five images at a time, I don't want a slider or pagination. I want all images in the dir loaded, but with a line break after every 5 images.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doutan3192 2015-08-20 15:30
    关注

    Add this to your loop to get the name of the image without the file extension

    <div class="row">
    <?php $dirname = "cards/"; $images = glob($dirname . "*.png");  ?>
    <?php foreach ($images as $image)  :?>
        <?php $title = pathinfo($image); ?>
        <div class="col-md-3">
          <img src="<?= $image ?>" alt="<?=title['filename']?>" class="img-thumbnail" >
        </div>
    <?php endforeach ?>
    </div>
    

    as far as displaying 5 images per row you want to create pagination and return 5 images each time you click on the link. look for jQuery slider plugin.

    Test produces the html markup below but only 4 images per row.

    <div class="container">
        <div class="row">                           
            <div class="col-md-3">
                <img src="downloads/hex_bg_black.jpg" alt="hex_bg_black" class="img-thumbnail">
            </div>          
            <div class="col-md-3">
                <img src="downloads/zh_bg_1.jpg" alt="zh_bg_1" class="img-thumbnail">
            </div>
        </div>
    </div>
    

    Hope this help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 扫描枪扫条形码出现问题
  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?