Welcome, Guest. Please login or register.
Did you miss your activation email?
February 12, 2012, 02:35:56 AM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
149621 Posts in 21098 Topics by 7537 Members
Latest Member: lotte2
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Random image to template css header  (Read 1797 times)
japla

Offline Offline

Posts: 18


« on: May 30, 2007, 05:20:55 PM »

Hello,

Any ideas how to call RandomImage from templates screen.css header definition?

Currently this part of my template is:
#header {
   text-align: left;
   width: 873px;
   height: 150px;
   background-color: #144e56;
   background-image: url(header.jpg);
   margin: 0 auto;
   border-top: 4px #FFFFFF solid;
   border-left: 4px #FFFFFF solid;
   border-right: 4px #FFFFFF solid;
   background-repeat: no-repeat;
}

Can I somehow call this RandomImage straight from this css, or do you have other ideas?

Thanks,

Janne
Logged
BerndJM

Offline Offline

Posts: 1764



« Reply #1 on: May 30, 2007, 05:55:11 PM »

Hi,

an easy way would be to write the part for the image as inline CSS in your template, like this:

Code:
<div id="header" style="background-image: url(<?php RandomImage ('path to your pics'); ?>)"></div>

Regards Bernd
Logged

In theory, there is no difference between theory and practice. But, in practice, there is.
japla

Offline Offline

Posts: 18


« Reply #2 on: May 30, 2007, 06:15:54 PM »

Thanks for help, anyway it seems that I still have a problem with syntax.

My line is inside index.php
<div id="header" style="background-image: url(<?php RandomImage ('//media/galleria'); ?>)">   </div>

When I look my webpages with this, at the place where picture shoud be, is just   )">

Any ideas, what went wrong?

Thanks,

Janne
Logged
BerndJM

Offline Offline

Posts: 1764



« Reply #3 on: May 30, 2007, 06:31:43 PM »

Oh, I see the mistake,

RandomImage gives back a string for the image tag with width and height values, which you can't use with the background-image  undecided

If you don't need it as a background-image you can try it with a normal img tag. Otherways ... have an idea, but will it check first.

Regards Bernd
Logged

In theory, there is no difference between theory and practice. But, in practice, there is.
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6819


WWW
« Reply #4 on: May 30, 2007, 06:41:54 PM »

If you use the CSS as inlin CSS (in the index file of your template), you can change at the bottom of the script:

Code:
echo '<img src="'.WB_URL.$dir.'/'.$image.'" '.$dimensions[3].'>';

This defines the output of the image. If you take away all the tag stuff and only leave the path the output should fit your needs (only in inline CSS though!):

Code:
echo 'WB_URL.$dir.'/'.$image;

Alternatively search the forum for "random background" (maybe together), I once adapted the oroginal script for that.

cheers

Klaus
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

BerndJM

Offline Offline

Posts: 1764



« Reply #5 on: May 30, 2007, 06:58:45 PM »

Oh Klaus,

couldn't you say that before I start thinking wink

Just made this modification in the include.php:
Code:
<?php

/* Random image snippet

   Call this nsippet with:

        RandomImage ('/media','img'); for normal img tag or
        RandomImage ('/media','bgi'); for background-image

   in your template

*/



function RandomImage($dir,$type) {


//read folder and get the picture names

$folder=opendir(WB_PATH.$dir.'/.'); 

while ($file readdir($folder)) 

$names[count($names)] = $file

closedir($folder);



//remove any non-images from array

$tempvar=0;

for ($i=0;$names[$i];$i++){

$ext=strtolower(substr($names[$i],-4));

if ($ext==".jpg"||$ext==".gif"||$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;}

}



//random

srand ((double) microtime() * 10000000);

$rand_keys array_rand ($names12);



//random image from array

$image=$names1[$rand_keys[0]]; 



//image dimensions

$dimensions GetImageSize(WB_URL.$dir.'/'.$image); 
// "normal" image or background ?
switch ($type) {
case "img" : echo '<img src="'.WB_URL.$dir.'/'.$image.'" '.$dimensions[3].'>';
break;
case "bgi" : echo WB_URL.$dir.'/'.$image;
break;
default : echo '<img src="'.WB_URL.$dir.'/'.$image.'" '.$dimensions[3].'>';
}

}

?>


Than it has to be called with a second parameter "img" for normal image or "bgi" for background-image. So you can use the function for both variants.

Regards Bernd
Logged

In theory, there is no difference between theory and practice. But, in practice, there is.
japla

Offline Offline

Posts: 18


« Reply #6 on: May 30, 2007, 07:26:21 PM »

Hello Again,

I think that you will soon kill me.

I first tried Klaus idea, and got error of wrong syntax. I believe that it was ' after echo, error went away but I was not able to make it work. I also tried to search, but no luck of finding the thread which you referred.

Then I tried Bernds idea, but still not working. No errors, no unwanted characters, but also no images.

Few questions:
* I removed #header definitions from screen.css. Is this correct?

* I added line <div id="header" style="background-image: url(<?php RandomImage ('/media/headerimages'); ?>)"> </div> to index.php. Is this correct?

From which point I should count the path for images, from template index.php folder or from RandomImage module folder? Anyway, I tried both, and no luck.

What happens is empty area at the place where images should be.

Any ideas?

Thanks,

Janne
 
Logged
BerndJM

Offline Offline

Posts: 1764



« Reply #7 on: May 30, 2007, 07:40:22 PM »

1.)
Quote
* I removed #header definitions from screen.css. Is this correct?

No thats not correct, you need it 'cause there are the other definitions for the header-div like width, height, background-repeat and so on

2.)
If you use my modified RandomImage function you must call it with the second parameter which says the function if you want img or background-image in your case:

Code:
<div id="header" style="background-image: url(<?php RandomImage ('/media/headerimages','bgi'); ?>)"> </div>

Regards Bernd

Logged

In theory, there is no difference between theory and practice. But, in practice, there is.
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6819


WWW
« Reply #8 on: May 30, 2007, 09:00:02 PM »

Here is the original post for random background: http://forum.websitebaker.org/index.php/topic,3578.msg26741.html#msg26741

And the code posted above should indeed look a bit different (I think like that):

Code:
echo WB_URL.$dir.'/'.$image;

cheers

Klaus
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

japla

Offline Offline

Posts: 18


« Reply #9 on: May 31, 2007, 04:27:26 AM »

Thank you all for helping me out!

It is now working well.

regards,

Janne
Logged
ruebenwurzel
WebsiteBaker Org e.V.

Offline Offline

Posts: 7660



WWW
« Reply #10 on: June 03, 2007, 02:50:09 PM »

Hello,

also have a look at the uccellini template:

http://addons.websitebaker.org/pages/templates.php?template=uccellini

This comes with a built in random header script.

Matthias
Logged
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!