A random image viewer i made - preCharge Forums
It shows that you are unregistered. Please register with us by clicking Here
preCharge Forums


Nav Green LeftNav Right
preCharge Forums > Website Design & Development > Programming > PHP » A random image viewer i made


Reply
Tcat Right
 
LinkBack Thread Tools Display Modes Tcat Right
Old 07-23-2006   #1 (permalink)
carnage
 
Posts: n/a
Default A random image viewer i made

this is a very simple php randome image viewer

it all works and i hope you can understand it
it is so simple you should be troubled
_________________________________________________
Quote:
<?php
//Make the image completely random.
srand((float) microtime() * 10000000);

//Array for the filenames.
$images = array("506254000_l.jpg", "493226254_l.jpg");

//Choose and image and echo it!
$image_key = array_rand($images);

//Just change the URL to the image location
echo '<img src="../images/'.$images[$image_key].'" border="0">';

?>
hope this is off help to people.. your Simple minded Genius Carnage
  Reply With Quote


Old 07-23-2006   #2 (permalink)
Jamie
Senior Member
 
Jamie's Avatar
 
Join Date: Jul 2006
Location: Swansea, Wales, UK
Age: 20
Posts: 239
Default Re: A random image viewer i made

Hey

Nice Script. Would you mind making it a teeny bit more accessible in its output. Remove the border="0" attribute, ad an alt attribute and close the <img/> tag. The it will be perfect

Jamie
__________________
Jamie is offline   Reply With Quote

Old 07-23-2006   #3 (permalink)
Seph
Junior Member
 
Seph's Avatar
 
Join Date: Jul 2006
Age: 20
Posts: 24
Default Re: A random image viewer i made

I have a few suggestions as well.

0) Use code tags instead of quote tags. =)

1) It's no longer needed to seed the random functions, so the srand() call it a waste of resources.

2) Perhaps create the image array dynamically by listing all files in a directory, this would be much more newbie friendly than having a user edit the file. php.net/glob will be perfect for that.
__________________
http://dndesign.dk/seph/sig.png
Seph is offline   Reply With Quote

Old 07-23-2006   #4 (permalink)
carnage
 
Posts: n/a
Default Re: A random image viewer i made

ok notes take but hear me out

1: it is supposed to be fore the simple minded
2: it works fine as it is
3: i wrote it nearly 2 years ago
4: if you want it take it use and and adapt it

just let me know if you use it and i will be greatful
  Reply With Quote

Old 07-23-2006   #5 (permalink)
Diplomat
 
Posts: n/a
Default Re: A random image viewer i made

Hah, Carnidge Is Dumb, Just Playin Bro.
  Reply With Quote

Old 07-23-2006   #6 (permalink)
Jamie
Senior Member
 
Jamie's Avatar
 
Join Date: Jul 2006
Location: Swansea, Wales, UK
Age: 20
Posts: 239
Default Re: A random image viewer i made

Code:
<?
function GetFiles($sdir){
   $files = array();
   $dir = opendir($sdir);
    while(($myfile = readdir($dir)) !== false){
        	if($myfile != '.' & $myfile != '..' & is_file($sdir."/".$myfile)){
            	$files[] = $myfile;
        	}else if($myfile != '.' & $myfile != '..' & is_dir($sdir."/".$myfile)){
				$filearr = GetFiles($sdir."/".$myfile);
					for($i=0;$i<count($filearr);$i++){
						$files[] = $sdir."/".$myfile."/".$filearr[$i];
					}
			}
    }
  closedir($dir);
  return $files;
}

$Files = GetFiles(".");
for($i=0;$i<count($Files);$i++){
echo $Files[$i]."<br/>";
}

?>
There is a script to get the files in the current and lower directories. HAIL Recursion. You will need to remove any non-image files from the array. A simple check can be done on each filename before it is added, this is a stimple string check.

Jamie
__________________
Jamie is offline   Reply With Quote

Old 07-23-2006   #7 (permalink)
Seph
Junior Member
 
Seph's Avatar
 
Join Date: Jul 2006
Age: 20
Posts: 24
Default Re: A random image viewer i made

Seems like an awful lot of code for such a simple task, perhaps http://www.php.net/glob could shorten it a bit. But hey, if it works then it works. =)
__________________
http://dndesign.dk/seph/sig.png
Seph is offline   Reply With Quote

Old 07-23-2006   #8 (permalink)
Jamie
Senior Member
 
Jamie's Avatar
 
Join Date: Jul 2006
Location: Swansea, Wales, UK
Age: 20
Posts: 239
Default Re: A random image viewer i made

The actual amount of is quite small, glob() could make it shorter I think if you were looking for a specific file type/name. My function just returns the entire contents of the directory including the contents of sub directories.

oh: ta for the glob tip never heard of it before.

Jamie
__________________
Jamie is offline   Reply With Quote

Old 07-23-2006   #9 (permalink)
abhisar
Junior Member
 
Join Date: Jul 2006
Posts: 27
Send a message via Yahoo to abhisar
Default Re: A random image viewer i made

make a file named image.txt and put links like this

::<img scr="http://abhisar.wen.ru/logo.gif" alt="logo"/>
::<img scr="http://krapwap.com/logo.gif" alt="logo"/>

now on page where you want to display random image insert the code

<?
$xfile = @file("image.txt");
$random_num = rand (0,count($xfile)-1);
$udata = explode("::",$xfile[$random_num]);
echo $udata[1]; //a random image from the text file will be displayed
?>
__________________
SIMPLY THE BEST
abhisar is offline   Reply With Quote

Old 07-23-2006   #10 (permalink)
Jamie
Senior Member
 
Jamie's Avatar
 
Join Date: Jul 2006
Location: Swansea, Wales, UK
Age: 20
Posts: 239
Default Re: A random image viewer i made

Quote:
Originally Posted by abhisar
make a file named image.txt and put links like this

::<img scr="http://abhisar.wen.ru/logo.gif" alt="logo"/>
::<img scr="http://krapwap.com/logo.gif" alt="logo"/>

now on page where you want to display random image insert the code

<?
$xfile = @file("image.txt");
$random_num = rand (0,count($xfile)-1);
$udata = explode("::",$xfile[$random_num]);
echo $udata[1]; //a random image from the text file will be displayed
?>
The only problem with that method is that it forced markup with content. which in my opinion is bad practice. It would be far better if image file names were stored externally to the markup. It would be far easier to maintain also, In your scenarios a user would have to upload a new image and create a new entry in image.txt. In a scenario using my code all the user has to do is upload a new image into the image directory and hey presto the script can use it.

Jamie
__________________
Jamie is offline   Reply With Quote

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
random sig tomtom111 Graphics & Multimedia 4 05-12-2006 09:21 PM
Random Stuff Shock v2.0 Graphics & Multimedia 2 09-27-2005 10:47 AM
Random Avatar threemad3 Graphics & Multimedia 7 09-10-2005 06:43 AM
Buy This Really Random Avator! Frozen Buy, Sell or Trade 1 06-08-2005 08:37 AM


footer left
All times are GMT. The time now is 09:35 PM.

DISCLAIMER: preCharge Risk Management is not responsible for any opinions, advice or comments expressed on the preCharge Community Forums.
preCharge® is a registered trademark of preCharge Risk Management | chargeback protection | Merchant Account Blog

Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC6

Web Advertising | Cell Phones | Per Insurance | Electricity Suppliers | Bad Credit Loan

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49