Jump to content


Random Images on webpage using php


18 replies to this topic

#1 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 03 January 2006 - 10:04 AM

Hello i am trying to follow a tutorial to create a random image on a page each time it loads, i think i have followed the tutorial but when i try the page i just get a red cross, can someone have a look and see if they could help me out? cos would be much appreciated.

www.retrodesigner.co.uk/index.html

and the tutorial is

http://mikemania.kaw...iewtut.php?id=7

i can post the code if that ouwld help anyone solve this problem, thanks

#2 Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 03 January 2006 - 10:38 AM

If you get a red X it means the path to your images is wrong. Double check the image url.

#3 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 03 January 2006 - 10:39 AM

View PostJaymz, on Jan 3 2006, 03:38 PM, said:

If you get a red X it means the path to your images is wrong. Double check the image url.


i have checked that and it seems to be okay

#4 Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 03 January 2006 - 10:48 AM

Post your code please.

#5 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 04 January 2006 - 07:15 AM

</head>

<body>

<div id="container"> 
	<div id="header">
	</div>
	
	<div id="menucontainer">
	<ul id="menu">
	  <li id="active"><a href="index.html" id="current">About RD</a></li>
	  <li><a href="Gallery.html">Gallery</a></li>
	  <li><a href="news.html">RD News</a></li>
	  <li><a href="contact.html">Contact RD</a></li>
	  <li><a href="Links.html">Links</a></li>
	</ul>
	</div>
	
	
  <div id="content">
	 <div id ="uppercontent">Retrodesigner are the latest team to bring you high quality handpainted art.  We are looking for more fans of the pop art revolution, so here at retrodesigner.co.uk you can see what we have to offer, by viewing our ever-growing gallery of original pop art.</div>
	 <div id ="middlecontent">
	 <img src="http://www.retrodesigner.co.uk/images/randomimages/random.php"> 
	 
	 </div>
	 
	<div id ="lowercontent">Enter the gallery to see <a href="Gallery.html">more 
	  »</a></div>
  
  </div>
	
	<div id="footer">All work sold remains the copyright of Retrodesigner © 2004
	</div>
</div>

</body>
>
and the php file code is

<?php
//directory here (relative to script)
$path = 'images\randomimages';
$i = 0;
$imgDir = opendir ($path);
while ( $file = readdir( $imgDir ) )
{
//checks that file is an image
$file_type = strrchr( $file, "." );
$is_image = eregi( "jpg|gif",$file_type );
if ( $file != '.' && $file != '..' && $is_image )
{ $images[$i++] = $file; }
}
closedir ($imgDir);
srand( (double) microtime()*1000000 );
$image_name = $path . '/' . $images[rand( 0,sizeof( $images ) -1 )];
$imgSize = GetImageSize( $image_name );
//ends script if no images found
if ( $i == 0 )
die();
//Display the image
readfile("$image_name");
?>
</html>


#6 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 04 January 2006 - 10:07 AM

leave your $path variable blank, only put a single slash in there like this
$path = '\';
problem is the script is looking for images in 'images/randomimages' while the script is already in there. So now its looking for images in 'images/randomimages/images/randomimages'.

Correct me if im wrong :)

#7 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 04 January 2006 - 10:39 AM

View PostAvalanche, on Jan 4 2006, 03:07 PM, said:

leave your $path variable blank, only put a single slash in there like this
$path = '\';
problem is the script is looking for images in 'images/randomimages' while the script is already in there. So now its looking for images in 'images/randomimages/images/randomimages'.

Correct me if im wrong :)


i am new to PHP and was just following a tutorial that said put a path there so i can try and change it see what happens, thanks

will post results

that doesnt seem to have worked

Edited by donkeymusic, 04 January 2006 - 10:47 AM.


#8 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 04 January 2006 - 12:33 PM

i tried it on my own server and it seemed to work fine :)

#9 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 04 January 2006 - 01:04 PM

View PostAvalanche, on Jan 4 2006, 05:33 PM, said:

i tried it on my own server and it seemed to work fine :ph34r:

do you get three scrolling images?

#10 Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 04 January 2006 - 01:15 PM

View Postdonkeymusic, on Jan 4 2006, 02:04 PM, said:

View PostAvalanche, on Jan 4 2006, 05:33 PM, said:

i tried it on my own server and it seemed to work fine :ph34r:

do you get three scrolling images?

That's not what the code is doing, it's selecting one random image.

#11 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 04 January 2006 - 01:27 PM

View PostJaymz, on Jan 4 2006, 06:15 PM, said:

View Postdonkeymusic, on Jan 4 2006, 02:04 PM, said:

View PostAvalanche, on Jan 4 2006, 05:33 PM, said:

i tried it on my own server and it seemed to work fine :ph34r:

do you get three scrolling images?

That's not what the code is doing, it's selecting one random image.
sorry thats what i meant but i dont see that image, originally was looking for a tutorial to allow me have scrolling images but could not find one

#12 Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 04 January 2006 - 01:35 PM

http://www.retrodesigner.co.uk/images/rand...ages/random.php

Quote

Parse error: parse error in /home/httpd/vhosts/retrodesigner.co.uk/httpdocs/images/randomimages/random.php on line 11

That's what it's not working :ph34r:

Edited by Jaymz, 04 January 2006 - 01:45 PM.


#13 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 04 January 2006 - 01:46 PM

View PostJaymz, on Jan 4 2006, 06:35 PM, said:

http://www.retrodesigner.co.uk/images/rand...ages/random.php

Quote

Parse error: parse error in /home/httpd/vhosts/retrodesigner.co.uk/httpdocs/images/randomimages/random.php on line 11

That's what it's not working :ph34r:


totally lost now aint got a clue what that means

#14 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 04 January 2006 - 02:02 PM

well find line 11 and see if theres a parse error.
Forgot to close your variables or w/e. Could be anything

#15 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 04 January 2006 - 02:43 PM

i have looked at line 11 and nothing seems to incorrect but as i know nothing about PHP its hard for me to see the error, the code is posted in earlier posts, if anyone can see the error there

#16 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 07 January 2006 - 08:24 PM

Try this, you were missing a bracket somewhere. :ph34r:

<?php
//directory here (relative to script)
$path = './images\randomimages';
$i = 0;
$imgDir = opendir ($path);
$images = array();
while ( $file = readdir( $imgDir ) )
{
	//checks that file is an image
	$file_type = strrchr( $file, "." );
	$is_image = eregi( "jpg|gif",$file_type );
	
	if ( $file != '.' && $file != '..' && $is_image )
	{ 
		$images[$i++] = $file; }
	}

	closedir ($imgDir);
	srand( (double) microtime()*1000000 );
	$image_name = $path . '/' . $images[rand( 0,sizeof( $images ) -1 )];
	$imgSize = GetImageSize( $image_name );
}
//ends script if no images found
if ( $i == 0 )
{
	die();
}
else
{
	//Display the image
	readfile($image_name);
}
?>

Post if it doesn't work, I have not tested it.

#17 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 08 January 2006 - 08:49 AM

View PostChaos King, on Jan 8 2006, 01:24 AM, said:

Try this, you were missing a bracket somewhere. :P

<?php
//directory here (relative to script)
$path = './images\randomimages';
$i = 0;
$imgDir = opendir ($path);
$images = array();
while ( $file = readdir( $imgDir ) )
{
	//checks that file is an image
	$file_type = strrchr( $file, "." );
	$is_image = eregi( "jpg|gif",$file_type );
	
	if ( $file != '.' && $file != '..' && $is_image )
	{ 
		$images[$i++] = $file; }
	}

	closedir ($imgDir);
	srand( (double) microtime()*1000000 );
	$image_name = $path . '/' . $images[rand( 0,sizeof( $images ) -1 )];
	$imgSize = GetImageSize( $image_name );
}
//ends script if no images found
if ( $i == 0 )
{
	die();
}
else
{
	//Display the image
	readfile($image_name);
}
?>

Post if it doesn't work, I have not tested it.

That didnt seem to work, what should the images be labelled as

Edited by donkeymusic, 08 January 2006 - 08:50 AM.


#18 _*Raremandan_*

  • Guests

Posted 08 January 2006 - 10:16 AM

<?php
//directory here (relative to script)
$path = './images/randomimages';
$i = 0;
$imgDir = opendir ($path);
$images = array();
while ( $file = readdir( $imgDir ) )
{
	//checks that file is an image
	$file_type = strrchr( $file, "." );
	$is_image = eregi( "jpg|gif",$file_type );
	
	if ( $file != '.' && $file != '..' && $is_image )
	{ 
		$images[$i++] = $file; }
	}

	closedir ($imgDir);
	srand( (double) microtime()*1000000 );
	$image_name = $path . '/' . $images[rand( 0,sizeof( $images ) -1 )];
	$imgSize = GetImageSize( $image_name );
}
//ends script if no images found
if ( $i == 0 )
{
	die();
}
else
{
	//Display the image
	readfile($image_name);
}
?>

I'm not sure but I think there was a directory error. Try that although i've not tested it!

- Dan

#19 donkeymusic

    Jedi In Training

  • Members
  • PipPip
  • 495 posts
  • Gender:Male
  • Location:Warrington, UK

Posted 08 January 2006 - 10:45 AM

i am willing to pass all my coding files to someone if they could sort this problem for me, if that would help anyone





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users