You are not logged in.
Hi,
I am working on a simple plugin that randomizes images… The only problem is caching… I had to turn that off via the admin in order to get my plugin to randomly display a new image.
Is there a better workaround for this? I would like to keep caching turned on, but that kills my plugin. :(
Has anyone found a “fix”?
Thanks!
Micky
Offline
mhulse wrote:
The only problem is caching… I had to turn that off via the admin in order to get my plugin to randomly display a new image.
Textpattern doesn’t cache pages in any miracle method. Actually it’s just Last-Modified header that is then threated as browser likes.
One way to fix this, is to write plugin that updates the Last-modified time in txp_prefs table. But if you use random images, then it’s quite hard to do, as it’s based on page loading. So, if you update the last-modified time on every page load, then it’s just same like turning the caching completely off.
Althought, you could add to you random image function the last-modified updater, in example: safe_update('txp_prefs','thefield_which_name_i_dont_remember',"now()") and so-on. But it just nullifies the “caching” for those pages/sections/articles where you use that plugin.
Last edited by Gocom (2008-04-21 05:54:45)
Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />
Online
Hi Gocom!
Thanks for the quick reply, I really appreciate it. :)
Great info too!
Hmmm, I may just shelf my plugin for now… Kinda seems like a JS solution could be more suitable — I like caching. :D
Back to the drawing board for me!
Thanks a billion Gocom!
Cheers,
Micky
Offline
fwiw, I have the same problem with smd_random_banner. When displaying random images, sometimes it just doesn’t do it with a simple refresh. When I use it, I always turn off send last modified header but sometimes I’d prefer not to.
I have thought of implementing an optional feature into the plugin to add a cache-buster on the end or do what Gocom suggested in prefs, but at the end of the day it seems most people would rather save download bytes than worry about an image that appears twice in succession on the same page. If anyone has any other thoughts on this, I’d be interested to hear them in case there’s something I’ve not thought of yet.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern.
Txp Builders – finely-crafted code, design and Txp
Offline
Here’s the random image function I use at a site I manage. I’m not sure it does what you need and I never notice any problems with caching but maybe I’m not paying enough attention. The function queries the image table for images in the “Random-Images” category and then builds an image tag. Perhaps it’s not the most flexible solution but that’s all I needed it to do.
function ccc_random_image() { $homepath = "http://".$GLOBALS['prefs']['siteurl'];$things = 'id, ext, w, h, alt, caption'; $where = "category = 'Random-Images'"; $imageArray = safe_rows($things, 'txp_image', $where);if ($imageArray) {$randomNumber = time() % count($imageArray); $randomImage = $imageArray[$randomNumber];$out = ''; $out .= '<img src="'.$homepath.'/images/'.$randomImage['id'].$randomImage['ext'].'"'; $out .= ' width="'.$randomImage['w'].'" height="'.$randomImage['h'].'"'; $out .= ' alt="'.$randomImage['alt'].'" />'."\n"; $out .= ' <p>'.$randomImage['caption'].'</p>'; }return $out; }
Offline
Thanks Gocom, Bloke, and jdykast — I really appreciate the help. :)
Thanks for sharing the code jdykast, that gives me some good ideas.
Have a great day all!
Cheers,
Micky
Offline
I use wet’s great wet_for_each_image plugin to get random images. It’s intended as “Yet Another Gallery Plugin,” but it actually does for images what article_custom does for articles. As such, you can just set the sort attribute to "rand()" to get a random item:
<txp:wet_for_each_image sort="rand()" limit="1" category="thingies" form="some_misc_form" />
Like jdykast, I haven’t noticed any caching issues. But like jdykast, I may just not be watching close enough.
Offline
for a few years now i’ve been using a random image script for the front page of hblack.net. It seems that – from my end at least – there no caching issues.Anyway… here’s the script
<?php//the directory$your_dirname="random/";//this is just the alt tag on top of the image$your_alt_tag="helene black";// /********************** ThE SCRIPT ************************************/function displayAaPHPrandomImage($dirname, $alt){ $dirhandle = opendir($dirname); while (false !== ($file = readdir($dirhandle))) { if ($file != "." && $file != ".." && !@is_dir($file)) { $filelist[] = $file; } }closedir($dirhandle); if(sizeof($filelist) ==0) { echo "No file was found in the directory!"; exit; }srand((double)microtime()*1000000); $picnum = @rand(0, sizeof($filelist) - 1); $imageName=$dirname.$filelist[$picnum]; $imageSize = getimagesize($imageName);$result="$imageName"; return $result; } ?><?=displayAaPHPrandomImage($your_dirname)?>
neme.org | neme-imca.org | hblack.net | LABS
Offline
Old thread, but I figured I’d post my really simple random image script:
<?phpfunction randImg($dir) { $dir = dir($dir); $img = array(); while(($file = $dir->read()) != false) { if (($file != '.') && ($file != '..')) { array_push($img, $file); } } $dir->close(); shuffle($img); $randKey = array_rand($img);return $img[$randKey]; }// for txp use - randImg(hu . $GLOBALS['img_dir']) echo '<img src="'. randImg('./img/') .'" alt=""/>';?>
Offline
jm, where do I put this code? in the html default page?
should I create an folder for the random images?
Offline