2

I have the following code that is functional that will randomize the photos I have in my 'photos' folder each time the refresh button is clicked. I know this may not be the most efficient way for this to be coded but for my matter it works. I am looking for help regarding my PHP code that will make the photos more random. I currently have 200+ pictures in the folder and often get repeated pictures more than I'd like. What changes to it can I make? (PS. ignore the AJAX/JavaScript I was playing around with)

<html> 

<head> 
<title>Pictures!</title> 
    <style type="text/css">
    body{ background-color:D3DFDE; }
    </style>
    <script type="text/javascript"      src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</head> 
<body> 
<div id='main'>
<?php 
function randomimages(){ 
    $dirname = isset($_REQUEST['dir'])? $_REQUEST['dir'] : './photos/'; 
    $numimages = isset($_REQUEST['num'])? $_REQUEST['num'] : 1; 
    $pattern = '#\.(jpg|jpeg|png|gif|bmp)$#i'; 
    $files = array(); 
    if($handle = opendir($dirname)){ 
        while(($file = readdir($handle)) !== false){ 
            if(preg_match($pattern, $file)){ 
                array_push($files, "<center><img src='" . $dirname . $file . "' alt='' /></br><br/><hr/></center>"); 
            } 
        } 
        closedir($handle); 
        shuffle($files); 
    } 
    return implode("<center><br/>", array_slice($files, 0, $numimages)) . "<br/> </center>"; 
} 
?>
<!-- <center><a id="myButton" href="#">MAS PICTURES!</a></center> -->
<center><input type='button' onClick='window.location.reload(true)' value='MAS PICTURES!!!' style="height:200px; width:150px" /></center>
<hr/>
<script type="text/javascript">
        $(function() {
      $("#myButton").click(function() {
        $("#main").load("index.php");
      });
    });
</script>
<?php echo randomimages(); ?>
<center>Created by: Matt & Joe</center>
</div>


</body> 
</html>
4

3 回答 3

2

You can do the following:

  1. Optimize the code by not reading the directory over and over. What you can do this by reading the directory once (and say then store the entries as an array in APC cache). Set a timeout for this APC key to bust the cache once in a while.
  2. Call the `mt_rand` function with min as `0` and max as `count(array)-1` and access that index.

Generic code to read from directory can be as follows (needs modification to match your needs):

<?php
function &list_directory($dirpath) {
    if (!is_dir($dirpath) || !is_readable($dirpath)) {
        error_log(__FUNCTION__ . ": Argument should be a path to valid, readable directory (" . var_export($dirpath, true) . " provided)");
        return null;
    }
    $paths = array();
    $dir = realpath($dirpath);
    $dh = opendir($dir);
    while (false !== ($f = readdir($dh))) {
        if (strpos("$f", '.') !== 0) { // Ignore ones starting with '.'
            $paths[] = "$dir/$f";
        }
    }
    closedir($dh);
    return $paths;
}
于 2013-11-14T06:49:53.597 回答
1

提供变量 $dirpath 的目录完整路径

$image_source_array=scandir($dirpath);
sort($image_source_array);

使用mt_rand具有 min as0和 max as 的函数count($image_source_array)-1并从数组中访问该索引以获取图像名称

然后使用 $dirpath/image 名称访问图像,您每次都会得到随机图像

像这样创建函数它将是最短的方法

function randomimages() {
    $dirname = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : './photos/';
    $image_source_array = scandir($dirname);
    sort($image_source_array);
    $image_count = count($image_source_array) - 1;
    $rand_index = mt_rand(3, $image_count);
    //Starting with 3 because scandir returns directory also in the 2 indexes like '.' and '..'
    $rand_image_path = $dirname . $image_source_array[$rand_index];
    return $rand_image_path;
}
于 2013-11-14T07:09:53.187 回答
0

为了简单和可重用性,您可能希望与RegexIterator一起使用DirectoryIterator

function randomimages($path, $num_images)
{
    $images = array();

    foreach (new RegexIterator(new DirectoryIterator($path),
                               '#\.(jpe?g|gif|png|bmp)$#i') as $file) {
        $images[] = $file->getPathname();
    }

    shuffle($images);

    return array_slice($images, 0, $num_images);
}

使用:

$path = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : './photos/';
$num_images = isset($_REQUEST['num']) ? $_REQUEST['num'] : 1;

print implode('<br />', randomimages($path, $num_images));
于 2013-11-14T07:54:55.877 回答