Rotating graphics

Aside from creating an animated gif - what would be a better approach for creating a rotating graphic on a home page. The graphic changes each time the home page is reloaded. There would be aproximately 50 images in a pool to choose from.


An Asset Listing in "Random" mode is the best way to do this. Set an Asset Listing to list Image assets and Random, and it'll automatically pick a random image every time it is loaded.
However, with caching enabled, you'll probably only get a random image each time the cache is generated (because it'll get cached). The only way to change the image on every load is to use JavaScript.

[quote]An Asset Listing in "Random" mode is the best way to do this. Set an Asset Listing to list Image assets and Random, and it'll automatically pick a random image every time it is loaded.
However, with caching enabled, you'll probably only get a random image each time the cache is generated (because it'll get cached). The only way to change the image on every load is to use JavaScript.[/quote]



thanks for quick reply - so i am presuming then that I can use the random asset listing as nested content


Absolutely. :)' /> My advice is to switch the all the bodycopies of the asset listing in Raw Presentation/Raw HTML mode so that you don't output masses of extraneous <div> tags. The onsite Squiz resource can help you with this if necessary. <img src='http://forums.matrix.squiz.net/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)

Gonna make a habit of hijacking threads rather than starting a new one :wink:


The asset listing produces HTML. Is there a way of forcing it to produce just the image sending the image header? - like this (5 mins script hacked together) does?



[codebox]<?php



$handle = opendir('/path/to/img/dir');



while(false !== ($file = readdir($handle))) {



if(($file != ".") && ($file != "…")) {

$files[] = $file;

$i++;

}

}



closedir($handle);



$path = '/path/to/img/dir/' . $files[mt_rand(0, $i-1)];



header ("content-type: image/jpeg");



$jpeg = fopen($path,"r");



$image = fread($jpeg,filesize($path));



echo $image;



?>

[/codebox]



Cheers



K

No, asset listing does not output raw file content.


We do this using javascript, as caching won't let you rotate images as Avi stated.

Page Contents:

    
    var theImages = new Array() 
%asset_listing%

var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.random();
whichImage = whichImage * (p-1);
whichImage = Math.ceil(whichImage);
function randomImage(){
document.write('<img src="'+theImages[whichImage]+'" alt="%asset_name%" />');
}
</script>

<script type="text/javascript">
randomImage();
</script></pre><br />

Image Format:

    theImages[%asset_position%] = '%asset_url%'


It works quite well this way. Any I am sure the javascript could be written better...:)

Can't you use %asset_contents% on an Image asset to get it to deliver the image itself?

Files print their content in printFrontend() so they don't have anything in printBody() for that keyword to output.


...well I was wanting to include it in CSS as a background image. (and I wouldn't cache it - exceptions defined in squid etc) thanks anyway.

K

I use an asset listing in this way to generate the URL to the image, instead of painting the image itself.