랜덤이미지

2 POSTS

  1. 2008/03/26 랜덤이미지 출력하기 (PHP) (1)
  2. 2004/09/22 랜덤 파일 출력

랜덤이미지 출력하기 (PHP)

Posted 2008/03/26 18:47, Filed under: Web-Tip
<?php
/*
    By Matt Mullenweg > http://photomatt.net
    Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
    Latest version always at:
    http://photomatt.net/scripts/randomimage
*/

// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
    foreach($exts as $ext) { // for each extension check the extension
        if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
            $files[] = $file; // it's good
            ++$i;
            }
        }
    }
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

$folder = '';
디렉토리 지정하는 부분과...

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';
확장자를 지정하는 부분

이 두가지 부분을 수정해주시고 php파일로 저장해 두시고 올려두면 지정한 폴더안에서 랜덤으로 이미지가 나옵니다..

그리고 사용은 <img src="http://www.올린주소.com/저장한.php"> 이렇게 사용하시면되겠습니다.

2008/03/26 18:47 2008/03/26 18:47

랜덤 파일 출력

Posted 2004/09/22 13:14, Filed under: Web-Tip

<?
$dir = opendir("./image"); // image라는 폴더에 랜덤으로 돌릴 파일을 첨부
while($a = readdir($dir))
{
   $b[] = $a;
}
$last = count($b);
$last = $last - 2; // -2를 하는 이유는 . 과 .. 을 빼주기위해. 
$aa = rand(1,$last);

?>

사용은 이렇게하세요.

<img src="<?=$aa?>" width="가로" height="세로">


2004/09/22 13:14 2004/09/22 13:14