2008-04-21 04:36:22

mhulse
Plugin Author
êta
Real name: Micky Hulse
From: Eugene Oregon

caching and random images

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

 

2008-04-21 05:52:54

Gocom
Developer
omega
Real name: Jukka Svahn
Website

Re: caching and random images

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 />

Offline

 

2008-04-21 06:13:36

mhulse
Plugin Author
êta
Real name: Micky Hulse
From: Eugene Oregon

Re: caching and random images

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

 

2008-04-21 07:46:24

Bloke
Developer
omega
Real name: Stef Dawson
From: Leeds, UK
Known languages: Engl(ish)
Website

Re: caching and random images

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

 

2008-04-21 11:59:33

jdykast
Member
zêta
Real name: Jason
From: Tennessee
Website

Re: caching and random images

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

 

2008-04-23 22:38:48

mhulse
Plugin Author
êta
Real name: Micky Hulse
From: Eugene Oregon

Re: caching and random images

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

 

2008-04-27 04:34:16

atbradley
Plugin Author
beta
Real name: Adam Bradley
From: Rhode Island, US
Known languages: en, some de
Website

Re: caching and random images

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

 

2008-04-27 06:11:18

colak
Admin
omega
Real name: Yiannis
From: Cyprus
Known languages: el, en
Website

Re: caching and random images

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)?>

Offline

 

2008-05-14 20:20:55

jm
Plugin Author
omega
Real name: Jon-Michael Deldin
From: Missoula, MT
Website

Re: caching and random images

Old thread, but I figured I’d post my really simple random image script:

<?php

function 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

 

2009-05-02 04:16:10

juliane
Member
beta

Re: caching and random images

jm, where do I put this code? in the html default page?

should I create an folder for the random images?

Offline

 

Powered by FluxBB