Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-04-21 04:36:22

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

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

#2 2008-04-21 05:52:54

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
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)

Offline

#3 2008-04-21 06:13:36

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

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

#4 2008-04-21 07:46:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

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. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#5 2008-04-21 11:59:33

jdykast
Member
From: Tennessee
Registered: 2004-12-30
Posts: 119
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

#6 2008-04-23 22:38:48

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

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

#7 2008-04-27 04:34:16

atbradley
Plugin Author
From: Rhode Island, US
Registered: 2007-02-15
Posts: 34
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

#8 2008-04-27 06:11:18

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

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

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#9 2008-05-14 20:20:55

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
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

#10 2009-05-02 04:16:10

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

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

#11 2009-05-02 17:38:40

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

Re: caching and random images

juliane wrote:

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

You can put it in your template or in an external file and include it. For the former, change <?php to <txp:php> and ?> to </txp:php>.

should I create an folder for the random images?

You could – it depends on what images you want to appear. If they’re your Textpattern images, just use:

echo '<img src="' . randImg(hu . $GLOBALS['img_dir'] . '" alt=""/>';

Does that help?

Offline

#12 2009-05-02 19:16:26

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

Re: caching and random images

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?
didnt quite understand when to change to <txp:php>.. is that if I put the code in the default html?

and how should I call it in the html file?
something like this: <img src=“random-img.php” alt=“Random Image” height=“150” width=“800” /> ?

thank you.

Offline

Board footer

Powered by FluxBB