duanlie1298 2017-01-02 22:48
浏览 62

在html中使用php将图像添加到幻灯片中

I'm trying to adjust a program for a TV screen at my local football club. I already made the site work, but now I want to make it more user friendly.

Specifically, I want to make the site work so someone can just add the images to a certain folder, and the slideshow will use all images in the folder.

The relevant part of the code is below; notice that the images are hard-coded:

<html>
    <head>
        <script type="text/javascript" src="js/tabs.js"></script>
        <script src="js/jquery.js"></script>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <script type="text/javascript" src="js/slider.js"></script>
    </head>
    <body>
        <div class="corner_1">
            <?php include("inc/script.php"); ?>
        </div>
        <div class="slider" id="slider">
            <ul>
                <li style="background-image:     url('http://homedir.com/images/IMG_4947.JPG'); no-repeat center center fixed; 
   background-repeat: no-repeat;
      background-size: 100% 100%;
    background-position: center top;
    background-attachment: fixed;">
                </li>
                <li style="background-image:     url('http://homedir.com/images/IMG_4939.JPG'); no-repeat center center fixed; 
   background-repeat: no-repeat;
      background-size: 100% 100%;
    background-position: center top;
    background-attachment: fixed;">
                </li>
                <li style="background-image:     url('http://homedir.com/images/IMG_4922.JPG'); no-repeat center center fixed; 
   background-repeat: no-repeat;
      background-size: 100% 100%;
    background-position: center top;
    background-attachment: fixed;">
                </li>
            </ul> 
        </div>
    </body>
    </html>

I tried to make something with the glob() and echo functions in PHP, but couldn't get it to work.

  • 写回答

1条回答 默认 最新

  • duanhai4046 2017-01-03 01:17
    关注

    You can do a scandir() easily enough:

    # I am just going to use this to clean up the html, you don't have to do this...
    $style = array(
        'background: no-repeat center center fixed',
        'background-repeat: no-repeat',
        'background-size: 100% 100%',
        'background-position: center top',
        'background-attachment: fixed'
    );
    $style = implode('; ',$style);
    # I use a define('ROOT_DIR',__DIR__) in a root-based config file to get the full path
    $path = '/var/www/vhosts/domain/images';
    # Scan the directory
    $dir  = scandir($path);
    # Loop through files in directory
    foreach($dir as $file) {
        # Use our root path to make sure file exists
        # (there are . and .. that will be in the array
        if(!is_file($path.'/'.$file))
            continue;
    ?>
            <li style="background-image: url('http://homedir.com/images/<?php echo $file ?>'); <?php echo $style ?>">
            </li>
    <?php
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀