Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#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
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
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
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
Re: caching and random images
jakob wrote:
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 imagesIf 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.
…. texted postive
Offline
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
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
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
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