-5

a long time ago i once did found a php script that contained only about 6 short lines of it if i remember correctly. Yet no luck, i couldn't find anything like that anymore, so i am gonna ask for help from you.

It was only one file "rotate.php" and a folder that contained all by myself chosen images, the script was supporting different dimensions and with different extensions (jpg, png, gif) as i remember.

Script generated one single url that could be used as an image url for an avatar or between [img] tags on forums with bbcode support.

Respectively, when someone visits my forums profile or sees my comment on forums, per every visit/refresh it always shows a random image as my avatar.

There seems to be many simple variations to create a random image slideshow for my own websites logo or something, but i can't figure a way to make it work for external sites like with previously mentioned single url.

Give me some hope, thanks.

4

1 回答 1

0

希望对您有所帮助,您可以使用调用opendir($path)的 PHP 内置函数打开路径中的文件夹并读取目录。

   <?php
        function getImagesFromDir($path) {
            $images = array();
            if ( $img_dir = @opendir($path) ) {
                while ( false !== ($img_file = readdir($img_dir)) ) {
                    // checks for gif, jpg, png
                    if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
                        $images[] = $img_file;
                    }
                }
                closedir($img_dir);
            }
            return $images;
        }

        function getRandomFromArray($ar) {
            mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
            $num = array_rand($ar);
            return $ar[$num];
        }

        $path = 'imagesfolder/';    

        $imgList = getImagesFromDir($root . $path);
        $img = getRandomFromArray($imgList);

     ?> 

在 HTML 中:

      <img src="<?php echo $path . $img; ?>" alt="" />

参考:http ://www.dyn-web.com/code/basics/random_image/random_img_php.php

于 2013-11-07T16:54:03.017 回答