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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?