Xtemplate: Need random background header image

rapture@coerce.net - January 4, 2004 - 05:29

I'm trying to display random background images in my xtemplate #header.

If it helps, I have http://cookedgamers.net/themes/xtemplate/images/banner/image.php spitting out file names of image files that I wish to use in the #header.

Does php run inside the .xtmpl file? I can't get php to run inside my .css file.

I feel I'm missing something very obvious. Can you guys help me out?

Thank you in advance.

Some pointers to get you started

Dries - January 4, 2004 - 08:38

You'll have to modify themes/xtemplate/xtemplate.theme and themes/xtemplate/xtemplate.xtmpl: the theme file must be modified to export a new variable, say {background}, to the template file. More specifically using the development version of Drupal, you have to add an extra field/line to the first $xtemplate->template->assign() statement in the xtemplate_header() function of themes/xtemplate/xtemplate.theme.

PHP is CSS

rapture@coerce.net - January 5, 2004 - 15:32

[ax: edited your comment to display properly (< --> &lt; etc.). please preview before posting!]

Thanks, Dries. If the following doesn't work, I'll attempt your method. Just found this on a forum and will test later.

"For PHP on an Apache server:

Add this line to your root .htaccess file:
AddType application/x-httpd-php .css
<!-- this tells the server to parse .css file for PHP commands -->

Add this line to *the top* of your .css file:
<?php Header ("Content-type: text/css");?>
<!-- this makes sure that the .css file looks like a regular .css file to anyone that asks. If you don't return a text/css header, Mozilla will refuse to include the file if the calling page has an XTML or strict HTML doctype -->

You can now jump into PHP mode anywhere in the .css file, use PHP logic (if, else etc.) and call PHP functions that return parameters to your CSS (which you can include into the CSS file in the usual manner)."

Got random banner working via css/php

rapture@coerce.net - January 6, 2004 - 03:22

I got php to parse inside my .css file by modifing my .htaccess file (see above).

I wrote a "image.php" file that echos the full URL of any image file in any certain folder.

In my .css that now parses php I have:

background-image: url(&lt;?php include("http://domain.net/path/to/image.php"); ?&gt;);

The image.php file writes out the URL of a random image. The "php include" command inserts that URL into the "background-image: url()" line.

Now, I tell .xtmpl to import the new style sheet:

@import url(http://domain.net/path/to/new-style-sheet.php);

Clever trick

Dries - January 6, 2004 - 08:13

That is a clever trick! :-) As the CSS file is no longer served as a static page, browsers might no longer be caching your site's CSS files though. If you are low on resources (bandwidth or computing power), this might be a concern.

i have been think bout having

bertboerland@ww... - January 5, 2004 - 16:40

i have been think bout having different logo's images as well. based on round robin, time of day (hence dark in night, light during day) and/or season greetings.

i think it can be easier done from the shell and use a random script for changing the image (cronned)

#!/usr/bin/perl
# cider@compulsion.org
# usage: random [some files]
#
# returns back random file from given list
# accepts wildcards
#
# example usage: Esetroot `random *.jpg`
#                mpg123 `random *.mp3`
srand;
@files = @ARGV;
$randomfile = @files[rand @files];
print "$randomfile\n";

otoh, i think the banner module is made for something like changing logo's since it can include round robin (not possible via cron based scripts) and have a relative weight (change) of the logo.

this module would be greatly enhanced if it could have some awareness of the environment. hence altering the change that a logo/banner is displayed based on:

  • * node viewed by date (for example advertise an agenda)
  • * same for forum topic (for example paid help)
  • * taxonomy (ad based on tax term)
  • * search words (ad based on key)
  • * local browser time (could be adult content banner)
  • * referring page (e.g when comming from search engine y promote x)
  • * etc...

--
groets

bertb

Another quite easy solution

geraldito - July 17, 2008 - 20:56

There is another really easy solution referring to a PHP file directly in CSS like: background: url(bg/random.php);

As result of the request the PHP file outputs the random image, important to add the content type to the HTTP header. Here's the file random.php:

<?php
$extList
= array();
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';

$img = null;

$fileList = array();
$handle = opendir('./');
while (
false !== ($file = readdir($handle))) {
   
$fileInfo = pathinfo($file);
    if (isset(
$extList[strtolower($fileInfo['extension'])])) {
       
$fileList[] = $file;
    }
}
closedir($handle);

if (
count($fileList) > 0) {
   
$imgNum = time() % count($fileList);
   
$img = $folder.$fileList[$imgNum];
}

if (
$img!=null) {
   
$imgInfo = pathinfo($img);
   
$contentType = 'Content-type: '.$extList[$imgInfo['extension']];
   
header($contentType);
   
readfile($img);
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.