负荷目录随机图像负荷、图像、目录

2023-09-10 14:02:03 作者:我比多肉更多肉

我想随机地从一个目录中加载图像,且有地方一个按钮,刷新整个页面。以下是目前的code我现在有:

I'd like to randomly load images from a directory and have a button somewhere that refreshes the entire page. Here's the current code I have now:

<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if (preg_match("/\.png$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
  }
  closedir($handle);
}

foreach ($a as $i) {

  echo "<img src='" . $dir . '/' . $i . "' />";

}

?>  

问题是,它加载所有400000图像一次。我只希望30加载。 30随机图像从目录中。我试图寻找一些code如修改上面这样的:

The problem is it loads all 400,000 images at once. I only want 30 to load. 30 random images from the directory. I tried looking up some code such as modifying the above to this:

<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if (preg_match("/\.png$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
  }
  closedir($handle);
}

foreach ($a as $i) {

  echo "<img src='" . $dir . '/' . $i . "' />";
  if (++$i == 2) break;

}

?>  

但似乎什么都不做。所以,如果有人可以帮助我从那个目录30随机照片,加载和有某种类型的刷新按钮,那将是很大的帮助。

But it seems to do absolutely nothing.. So if someone can help me get 30 random photos from that directory to load and have some type of reload button, that would be of great help.

感谢你在前进

推荐答案

这是我的一个缓存解决方案:

Here is my solution with a cache:

<?php

define('CACHE_FILE', 'mycache.tmp');
define('CACHE_TIME', 20); // 20 seconds (for testing!)
define('IMG_COUNT', 30);
define('IMG_DIR', '../public/wp-content/uploads/2012/01');

/**
  * Loads the list (an array) from the cache
  * Returns FALSE if the file couldn't be opened or the cache was expired, otherwise the list (as an array) will be returned.
  */
function LoadListFromCache($cacheFile, $cacheTime)
{
  if ( file_exists($cacheFile) )
  {
    $fileHandle = fopen($cacheFile, 'r');
    if ( !$fileHandle )
      return false;

    // Read timestamp (separated by "\n" from the content)
    $timestamp = intval( fgets($fileHandle) );
    fclose($fileHandle);
    // Expired?
    if ( $timestamp+$cacheTime > time() )
      return false;
    else
    {
      // Unserialize the content!
      $content = file_get_contents($cacheFile);
      $content = substr( $content, strpos($content, "\n") );

      $list = unserialize($content);
      return $list;
    }
  }
  return false;
}

/**
  * Caches the passed array
  * Returns FALSE if the file couldn't be opened, otherwise TRUE.
  */
function SaveListToCache($cacheFile, $list)
{
  $fileHandle = fopen($cacheFile, 'w');
  if ( $fileHandle === FALSE ) return false;

  fwrite($fileHandle, time());
  fwrite($fileHandle, "\n");
  fwrite($fileHandle, serialize($list));

  fclose($fileHandle);
  return true;
}

/**
  * Generates the list of all image files (png, jpg, jpeg) and caches it.
  * Returns the list as an array.
  */
function GenerateList()
{
  $a = array();
  $dir = IMG_DIR;
  if ($handle = opendir($dir))
  {
    while (false !== ($file = readdir($handle)))
    {
      if (preg_match("/\.png$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
    }
    closedir($handle);
  }
  SaveListToCache(CACHE_FILE, $a);
  return $a;
}

function GetRandomImages($list, $count)
{
  $listCount = count($list);
  $randomEntries = array();

  for ($i=0; $i<$count; $i++)
  {
    $randomEntries[] = $list[ rand(0, $listCount) ];
  }
  return $randomEntries;
}

// This code will execute the other functions!

$list = LoadListFromCache(CACHE_FILE, CACHE_TIME);

if ( $list === FALSE )
{
  $list = GenerateList();
}
$images = GetRandomImages($list, IMG_COUNT);

foreach ($images as $image)
{
  echo '<img src="', IMG_DIR.DIRECTORY_SEPARATOR.$image, '" />';
}