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.

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

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)