Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Displaying random images
Old 02-20-2009, 09:54 PM Displaying random images
itscooper's Avatar
Super Talker

Posts: 147
Location: Herts, UK
Trades: 0
Okay, this has been bugging me. I've been making some changes to a client's site, and I'm trying out a little time-saver... since it would take me years to manually change it.

Firstly, here's the HTML for an image used hundreds of times throughout the site:

Code:
<img src="images/no_image.gif" />
I've changed the .htaccess file to redirect the image to a PHP file called 'randImg.php' like so:

Code:
RewriteEngine on
RewriteRule ^images/no_image.gif$ randImg.php
My 'randImg.php' uses GD to output one of four new random images:

Code:
<?PHP

header("Content-type: image/png");

$temp = rand(1,4);
$path = "images/myauction/fish".$temp.".png";
$i = imagecreatefrompng($path);
imagepng($i);
imagedestroy($i); 

?>
Firstly, this pretty much works (shocker I know... at least it was for me). Every time I refresh the page, I get 1 of the 4 images randomly.

HOWEVER... If there is more than one of these 'no_image.gif's on each page, it displays the same image for each of them.

I thought it might have something to do with the browser assuming each image will be the same, but I don't know how to get round this.

Not sure where to go... any help greatly appreciated.
__________________
itsCooper
itscooper is offline
Reply With Quote
View Public Profile Visit itscooper's homepage!
 
 
Register now for full access!
Old 02-21-2009, 05:30 AM Re: Displaying random images
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
It's a user based caching problem. The user download a file, it's stored then next time the same URL is referred to the image will be the one stored on the users PC.

You could add some headers such as 'no cache', but on a single page it's a bit of a pain

Try:

PHP Code:
<?PHP
header
("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header'Pragma: no-cache' );    
header("Content-type: image/png");

$temp rand(1,4);
$path "images/myauction/fish".$temp.".png";
$i imagecreatefrompng($path);
imagepng($i);
imagedestroy($i);
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 02-22-2009 at 10:13 AM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 02-21-2009, 05:40 PM Re: Displaying random images
itscooper's Avatar
Super Talker

Posts: 147
Location: Herts, UK
Trades: 0
Thanks rogem002,

I'm gonna try something else. After a little more research you're obviously right.

The browser would see each image as different if they all had different image sources. So if the images looked like this:

Code:
<img src='randImg.php?id=1' />
...and the id variable changed for each... then it would assume each image was different. The id variable itself isn't used, it would just be there to fool the browser

I still uphold that I'm not going to trawl through the code manually, so I'll be storing the pages in the buffer (ob_start() in prepend file) and at the end replacing any 'no_image.gif' files with the php script above. Simple... I hope.

Any quicker and easier solution readily accepted... I'll let you know how i get on.

Thanks
__________________
itsCooper
itscooper is offline
Reply With Quote
View Public Profile Visit itscooper's homepage!
 
Old 02-21-2009, 11:11 PM Re: Displaying random images
Super Talker

Posts: 102
Trades: 0
http://scriptplayground.com/tutorial...-Image-Loader/
Aaron™ is offline
Reply With Quote
View Public Profile
 
Old 02-22-2009, 07:58 AM Re: Displaying random images
itscooper's Avatar
Super Talker

Posts: 147
Location: Herts, UK
Trades: 0
Thanks for the link Aaron!

Unfortunately it's not generating the random image that I'm having trouble with (my thread title was sort of misleading I suppose). What I'm having problems with is is generating the random image in place of 'no_image.gif'!

Thanks again
__________________
itsCooper
itscooper is offline
Reply With Quote
View Public Profile Visit itscooper's homepage!
 
Old 02-22-2009, 09:14 AM Re: Displaying random images
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
Simple solution, add a date code to the name of the file!
__________________
Sleeping Troll, EMUSE, Mind Expansion...Truly serendipity!
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Old 02-22-2009, 10:09 AM Re: Displaying random images
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Do you think redirecting to the image instead of including it would work? Like:

PHP Code:
<?PHP


$temp 
rand(1,4);
$path "images/myauction/fish".$temp.".png";

header('location: http://www.site.com/'.$path)

?>
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Reply     « Reply to Displaying random images
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37981 seconds with 12 queries