【问题标题】:include loading webpages in slideshow包括在幻灯片中加载网页
【发布时间】:2017-06-14 18:14:43
【问题描述】:

我对 stackoverflow 完全陌生。我正在尝试调整一个现有脚本,其中包含照片和视频的文件夹正在加载并像幻灯片一样显示。

我还想添加加载网页的选项。有什么简单的方法吗?

非常感谢。 这是我的代码:

<?php

include "class.getFiles.php";

$images = new getFiles();
// list of all files in the images folder (includes videos)
$imageArray = $images->getImageArray();

$sortedImages = new sortFiles();
$sortedImages->sortImageArray($imageArray);

// remove files not in the correct time period
$imageArray = $sortedImages->getImageArray();
$randImage = $sortedImages->randomImageNum();

$fileName = $imageArray[$randImage];

$info = new SplFileInfo($fileName);



?>
 <!DOCTYPE html>
<html>
<head>
<title>Fiction Slideshow</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>

<body>
<?php 

if($info->getExtension() == "mp4")
{
    echo '<video id="vid" class="videoDisplay" autoplay>
  <source src="images/'.$fileName.'" type="video/mp4">
  Your browser does not support the video tag.
</video>';

   echo '<script type="text/javascript">
    var vid = document.getElementById("vid");
    vid.addEventListener("ended", function(){
        window.location.reload();
    });
   </script>';

}
else
{
    echo '<img class="imageDisplay" src="images/'.$fileName.'" />';
    echo '<script type="text/javascript">
    setTimeout(function(){
        window.location.reload();
    }, 30000);
   </script>';

}
 ?>
</body>

</html> 

这是其他脚本调用的 class.getFiles.php 文件。

<?php

类 getFiles{

protected $dir;
protected $imageArray;


function __construct()
{
    $this->get_dir();
    $this->get_images();
}

protected function get_dir()
{
    $this->dir = getcwd();
}

protected function get_images()
{

    if(count(scandir($this->dir."/images")) != 2)
    {
        $this->imageArray = scandir($this->dir."/images");
    }
    else
    {
        die("There are no files in the directory");
    }

}



public function getImageArray()
{
    return $this->imageArray;
}

}

类排序文件{

protected $sortedImageArray = [];


public function sortImageArray($imageArray)
{
    foreach ($imageArray as $imageFile ) 
    {  
        if($imageFile !== ".." && $imageFile !== ".")
        {

        $imagePath = $imageFile;
        $imageFile = (substr($imageFile, 0, -4));
        $BeginningPos = strpos($imageFile, '_');
        $beginningDate = (substr($imageFile, 0, $BeginningPos));

        $beginningDateformatted = str_replace("-","/", $beginningDate);

        $stringToStartTime = strtotime($beginningDateformatted);


        $EndingPos = strpos($imageFile, '_', $BeginningPos  + strlen('_'));
        $EndingPos = $EndingPos + 1;
        $EndingDate = (substr($imageFile, $EndingPos));
        $EndingDateformatted = str_replace("-","/", $EndingDate);
        $stringToEndTime = strtotime($EndingDateformatted);

        $time = time();

        if($time <= $stringToStartTime && $time >= $stringToEndTime)
            {
                array_push($this->sortedImageArray, $imagePath);

            }

        }

    }

}

public function getImageArray()
{
    if(count($this->sortedImageArray) != 0)
    {
        return $this->sortedImageArray;
    }
    else
    {
        die("There are no files in the time range");
    }
}

public function randomImageNum()
{
    $imageArrayLength = count($this->sortedImageArray);
    $imageRand = rand(0, $imageArrayLength-1);
    return $imageRand;
}

}

?>

【问题讨论】:

    标签: php slideshow


    【解决方案1】:

    您可以将网页保存为媒体文件夹中的链接。例如,如果你想显示网页http://www.example.com,那么你可以在媒体文件夹中创建一个文件webpage1.txt。这个文件可以有链接http://www.example.com

    在您的代码中,您可以添加一个新条件来检查文件类型是否为 txt。如果文件类型是 txt,那么您的代码可以读取文件内的链接并使用 iframe 显示链接。以下代码应该可以工作:

    else if(strpos($fileName, "weblink") !== false)
    {
       /** The path to the web page link */
       $file_path     = (getcwd() . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . $fileName);
       /** Read contents of file */
       $web_page_link = file_get_contents($file_path);
       /** Display the link in iframe */
       echo "<iframe src='".$web_page_link."' width='100%' height='100%'></iframe>";
    }
    

    上面的代码应该在 else 语句之上

    【讨论】:

    • 非常感谢 Nadir。我要试一试,如果它有效,我会告诉你。再次感谢您!
    • 它没有用。我认为这是导致问题的“class.getFiles.php”。我将把代码放在我原来的问题中的那个文件中。你能帮我弄清楚为什么它不起作用吗?感谢您的宝贵时间。
    • 尝试将网页文件放入名为“webpages”的文件夹中。再次尝试上面的代码。我已经更新了它。它应该高于您发布的第一个示例文件中的最后一个 else 语句。
    • 亲爱的 Nadir,原来我自己犯了一个错误。原始脚本使用的代码行使得为图像文件夹中的所有文件提供唯一名称以正确显示是必要的。例如:12-31-2017_Example-Slide6_01-01-2015.png.txt。该脚本现在在幻灯片中加载 iframe,甚至无需制作整个网页文件夹。现在的问题是我看到幻灯片正在加载 iframe,但它不起作用。我只添加了 txt 文件的链接,如下所示:'http://www. stackoverflow.com'。我需要做任何改变吗?感谢您的宝贵时间!
    • @JD10081986。不客气。我只是想帮忙。加载 iframe 时,您在幻灯片中遇到什么错误?
    猜你喜欢
    • 2017-08-14
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多