dslreports logo
 
    All Forums Hot Topics Gallery
spc
Search similar:


uniqs
387

Mike D0
I want your skulls
Premium Member
join:2001-09-12
Harrisburg, PA

Mike D0

Premium Member

Changing a photo on page reload.

I am looking for a way to change a photo on page reload. I'm thinking this can be done with javascript. I'm not really looking for anyone to post code maybe just point me in the right direction. I searched google but I'm not really sure how to phrase the search to yield any relative results.

Thanks in advance for any help.

Gwellin
Premium Member
join:2004-05-31
Regina, SK

Gwellin

Premium Member

Random Image JavaScript

First result looks good, although I would personally use PHP to do that.

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD
ARRIS SB6141
Ubiquiti EdgeRouter Lite
Ubiquiti UniFi AP

1 recommendation

JAAulde to Mike D0

MVM

to Mike D0
said by Gwellin:

...although I would personally use PHP to do that.
Me too.
said by Mike D0:

I'm not really looking for anyone to post code...
I just can't help myself. ;)
php code:
<?php
// CONFIGURATION SECTION
//
//
//Which types of files should we allow to be in the list from which we will randomly choose?
$extensionsToAllow = array(
'jpg',
'gif',
'png'
);
//Should the above file extensions be handled case sensitively?
$extensionsCaseSensitive = false;
//
//
// NO FURTHER CONFIGURATIONS - DO NOT EDIT BELOW THIS LINE

$cwd = dirname(realpath(__FILE__));
if(is_dir($cwd)){
$openedDir = @opendir($cwd);
if($openedDir !== false){
$URItoDir = 'http://'.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'],'',$cwd).'/';
$fileList = array();
while(false !== ($file = readdir($openedDir))){
if($file!='.' && $file!='..' && $file!=basename(__FILE__)){
$extensionsCaseSensitive===false ? $filename = strtolower($file) : $filename = $file;
foreach($extensionsToAllow as $ext){
if($extensionsCaseSensitive===false){
$ext = strtolower($ext);
}
if(substr($filename,-(strlen($ext)))==$ext){
$fileList[] = $file;
break;
}
}
}
}
closedir($openedDir);
if(count($fileList)>0){
$fileURI = $URItoDir.$fileList[array_rand($fileList)];
echo "<img src=\"$fileURI\" alt=\"\" />";
}
}
}
?>
1) Create a directory on your server to hold the images which should be randomly selected.
2) Place a file with the above PHP code in that directory along with the images.
3) Adjust the configuration lines if necessary (I doubt it will be necessary...most of this is self configuring.)
4) From anywhere on your site, include() the code file from step 2 and one of your images should be randomly outputted.

Jim

rjackson

join:2002-04-02
Ringgold, GA
Netgear R6400
Switches Trash Bin
Apple AirPort Extreme (2011)

2 recommendations

rjackson

Nice, I recently did a "random image" script in PHP too (kittens, actually). This one uses scandir(), a PHP5 function. It was written really quick, and pulls out all the image files (determined by MIME type) from the given directory.
php code:
<?

$doc_root = "/var/www";

$kitten_dir = "/kittens/";

$contents = scandir($doc_root . $kitten_dir);

foreach($contents as $file)
{
if(strpos(mime_content_type($doc_root . $kitten_dir . $file), "image") === 0)
{
$kittens[] = $file;
}
}

$total = count($kittens) - 1;

$rand_num = rand(0, $total);

if(count($kittens) > 0)
{
echo "<img src=\"" . $kitten_dir . $kittens[$rand_num] . "\"/>";
}

?>

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD
ARRIS SB6141
Ubiquiti EdgeRouter Lite
Ubiquiti UniFi AP

JAAulde

MVM

I love PHP5 relative to PHP4. I cannot wait for it to become more mainstream. I suspect that once Red Hat Enterprise moves forward to PHP5 we'll see a large migration. As it stands, RHEL4 uses PHP4 and that is not going to change, causing many enterprise operations to be stuck in PHP4 mode. We're also going to need to see hosts do one of two things for a long while...either advertise and market PHP5 packages while still providing PHP4 packages until a certain time, or do side by side installs with different extensions being parsed by different versions--like PHP3 and PHP4 during their transition. The latter is complicated, though.

Gwellin
Premium Member
join:2004-05-31
Regina, SK

Gwellin

Premium Member

said by JAAulde:

I just can't help myself.
You ask on a Developers forum and that's what you get. I was tempted to post a PHP method too, but I didn't have time.

I love PHP5 too, scandir() being one of the reasons.

Mike D0
I want your skulls
Premium Member
join:2001-09-12
Harrisburg, PA

Mike D0

Premium Member

Thanks so much guys. Awesome as usual. I'm totally cool with using php. I just assumed something like that would be done using javascript. Thanks again.