Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2009-05-02 19:51:58

juliane
Member
Registered: 2009-02-08
Posts: 33

Re: caching and random images

i found this tutorial: http://sonspring.com/journal/easy-random-css-backgrounds

it worked. I had to put the rotator.php in the same folder as the images and create a class in css then call it in the html file.

Offline

#14 2009-05-02 21:01:12

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: caching and random images

juliane wrote:

I wanted to create a folder called “random” for the images. they are not my article images.

so I created a php file with your code. and saved it in the textpattern folder. is that where I should save it?

It can be anywhere (outside the www/htdocs root, for instance).

didnt quite understand when to change to <txp:php>.. is that if I put the code in the default html?

Yes.

and how should I call it in the html file?

<?php echo '<img src="'. randImg('./random/') .'" alt=""/>'; ?>

./random/ means the rotator script exists alongside the random directory.

Offline

#15 2009-05-03 03:00:46

TheEric
Plugin Author
From: Wyoming
Registered: 2004-09-17
Posts: 566

Re: caching and random images

FYI – a simple way to eliminate any browser side caching is this : src=“imageDirectory/image.jpg?randomTextOrNumbers”

Offline

#16 2011-05-16 22:09:32

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,075
Website Mastodon

Re: caching and random images

hmmm. i have a rotator script installed which is working fine, but for one behaviour. When I go to the section Events, for example, it provides a random image. but when I subsequently go the other sections the images do not change. I have to do a browser refresh to get a different image in each section

Ideally I would like to have a random image appear every time i go from one section to the other AND when I choose one of the various different categories within each of the section

Is this possible, at least for the section to section option?


…. texted postive

Offline

#17 2011-05-16 22:38:17

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: caching and random images

If your script is choosing a random image from the database when call up a page, I’m not sure why it should always give you the same image unless you are using some kind of page caching that presents the last cached version of the page without executing the code.

Otherwise, have you tried The Eric’s solution? If you generate a sufficiently long random number, or a number from the current time the number will always be different and the browser thinks it’s loading a different image. Again, this will only work if the page code is actually processed.


TXP Builders – finely-crafted code, design and txp

Offline

#18 2011-05-16 22:48:39

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,075
Website Mastodon

Re: caching and random images

jakob wrote:

If your script is choosing a random image from the database when call up a page, I’m not sure why it should always give you the same image unless you are using some kind of page caching that presents the last cached version of the page without executing the code. Otherwise, have you tried The Eric’s solution? If you generate a sufficiently long random number, or a number from the current time the number will always be different and the browser thinks it’s loading a different image. Again, this will only work if the page code is actually processed.

Hi – Thanks for reply. The link to Eric’s solution is not working. ps. i am using a rotator.php script that is outside the txp database, and resides in my ./otherimages/random/ dir. where i have 30 images

…. texted postive

Offline

#19 2011-05-16 23:24:50

atbradley
Plugin Author
From: Rhode Island, US
Registered: 2007-02-15
Posts: 34
Website

Re: caching and random images

bici, it sounds like your browser is caching the image. Try adding this near the top of your script (before you start sending the image):

header("Cache-control: no-cache");

Offline

#20 2011-05-17 03:56:57

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,075
Website Mastodon

Re: caching and random images

atbradley wrote:

bici, it sounds like your browser is caching the image. Try adding this near the top of your script (before you start sending the image):

header("Cache-control: no-cache");

Thank-you
yep that did the trick… at least in firefox. but not in Safari.. oh well

ps; i added this …

header("Cache-Control: no-cache, must-revalidate");

Last edited by bici (2011-05-17 04:09:06)


…. texted postive

Offline

#21 2011-05-17 07:44:14

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: caching and random images

The link to Eric’s solution is not working.

It was one post above your own post in this thread.

ps. i am using a rotator.php script that is outside the txp database, and resides in my ./otherimages/random/ dir. where i have 30 images

Oh, ok. If you can show what script you are using, maybe we can fudge the random number into the script. Here are some pointers if you want to have a go yourself.


TXP Builders – finely-crafted code, design and txp

Offline

#22 2011-09-01 05:21:18

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,075
Website Mastodon

Re: caching and random images

trying it on another site and different server and the random script still not working. it caches the images and i need to refresh the page to see new image.

my code:
<img src="../assets/images/rotator2/rotate.php?<?php echo(time());?>" />

the rotate script:

<?php

header("Cache-Control: no-cache, must-revalidate");

/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > https://github.com/dan/hivelogic-image-rotator
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. 
/* usage <img src="/dropbox/2003/rotate/rotate.php" alt="A Random Image" /> */

$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!

?>

Added some formatting (bc..) for the code -Gocom.

Last edited by Gocom (2011-09-01 08:08:05)


…. texted postive

Offline

Board footer

Powered by FluxBB