English 中文(简体)
Load Random 图表
原标题:Load Random Images from Directory

我随意从目录中排出图像,在某个地方有一个纽芬兰语,以更新整个网页。 这里,我现在有:

<?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 . "  />";

}

?>  

问题在于它一劳永逸地装上了40万幅图像。 我只想有30人。 该名录有30幅随机图像。 我试图研究一些法典,例如对上述法律进行修改:

<?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个随机照相,装上某些类型的后载纽吨,那将大有帮助。

事先感谢你

最佳回答

这里,我的解决办法是:

<?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 "
" 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, "
") );

      $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, "
");
  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,  " /> ;
}
问题回答

如果你有400 000幅图像,那么我认为,每次阅读整个目录都将是显示随机图像的一种昂贵手段。 我将使用一个数据库,并储存其档案途径。

如果你想使用你现有的法典,那么就想这样做。 您有一系列长篇<代码>n,其中载有图像名称。 请注意rel=“nofollow”>generate 30 arbitrary number between 0 and n-1。 然后在阵列中展示与这一位置相关的形象。 I m 不是加拿大专家,但这里是一些假装:

$a = array(); 
$dir =  ../public/wp-content/uploads/2012/01 ;

if (preg_match("/.png$/", $file)) $a[] = $file;
elseif (preg_match("/.jpg$/", $file)) $a[] = $file;
elseif (preg_match("/.jpeg$/", $file)) $a[] = $file;

for ( i=0; i < 30; i++) {
     //generate a random number between 0 and N-1
     random = rand(0, $a.length - 1);
     //display that image in the array
     echo "<img src= " . $dir .  /  . $a[random] . "  />";
}

You need to create a new variable for the counter instead of using $i

例如,你可以这样做。

$j = 0;
foreach ($a as $i) {
  echo "<img src= " . $dir .  /  . $i . "  />";
  $j++;
  if ($j >= 30)
  {
    break;
  }

}

www.un.org/Depts/DGACM/index_spanish.htm 也许就随机部分而言,你首先可以生成0至1的随机数字,其中只有N是图像总数,然后才用指数编号反映阵列的形象。

我不使用<条码>foreach,而是认为你需要<条码>代替>。

$totalImgs = count($a);
$imgUsed = array();
for ($j = 0; $j < 30; $j++)
{
    do
    {
        $randIndex = mt_rand(0, $totalImgs);
    }
    while ($imgUsed[$randIndex] === TRUE);
    $imgUsed[$randIndex] = TRUE;
    echo "<img src= " . $dir .  /  . $a[$randIndex] . "  />";
}

你可能只读到你的名录中的30份档案。 在读物者假返回或阵列时间满30时停止在册。

工作

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

它可能不执行(我没有尝试过)。 但这里的想法是:

随机抽取图像:sh($a)应做trick

in simplest way , you can use

find , sort , head 

二级指挥,与PHP在建构

exec()

function to get 30 random image links easily , the folowing snippet lists how to do it (How to get random 30 image links in an array.)

<?php
$picdir = "directory/containing/pictures"; // directory containing only pictures
exec("find " . $picdir . " | sort -R | head -30 ",$links);
while(list($index,$val) = each($links) ) {
    echo "<img src =" .$val . "> <br/>";  // shows image 
}

?> Here $links array contain random 30 image names(from folder) with complete path . This is used with img tag in echo to generate images

在这里,有图像的目录有线索,假定透镜只有图像档案。 在其他情况下,它的唯一事项是修改指挥,以排除非图像档案(例如利用灰色指挥排除)





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签