Log in

View Full Version : Random background image with PHP


stryka
07-03-2012, 09:20 PM
I currently have a PHP function that does a directory scan and randomly pulls an image to appear in the background CSS tag...

It essentially creates the following entry before the BODY tag

<style type="text/css">
body {
background-image: url(http://www.sitename.com/img/bg/IMG_1461_edit.jpg);
}
</style>

Any idea how i can do this in VB3.8?

Thanx

kh99
07-03-2012, 10:12 PM
Try creating a new plugin using hook parse_templates and code like this (insert your function at the comment):


ob_start();
// call your function here
$str = ob_get_contents();
ob_end_clean();

$vbulletin->templatecache['headinclude'] .= addcslashes($str, '\\"');

stryka
07-04-2012, 09:55 PM
hmm... I think I lost you... what value does $str need to be for it to work?

is this the value i am sending

background-image: url(http://www.sitename.com/img/bg/IMG_1461_edit.jpg);

kh99
07-04-2012, 10:03 PM
$str would have to be the entire thing you posted in the first post. If your function is only returning the part you posted above, then try this:

ob_start();
// call your function here
$str = ob_get_contents();
ob_end_clean();

$str = '<style type="text/css">
body {
' . $str . '
}
</style>
';
$vbulletin->templatecache['headinclude'] .= addcslashes($str, '\\"');


Also, I was assuming that your function output a string. If it returns a string, then you don't need the ob_start()/ob_end_clean() calls.

stryka
07-04-2012, 10:24 PM
ahh... so my plugin would look like this...




function get_random_image() {

$files = scandir($_SERVER['DOCUMENT_ROOT'].'/app/webroot/img/bg');
$count = count($files);
srand(time());
$img = $files[rand(0,$count)];
if(($count-2) <= 0) return FALSE;
return $img;

}

function get_random_image_ex() {
do {
$img = get_random_image();
if($img === FALSE) return FALSE;
if($img != '.' && $img != '..' && !empty($img)) break;
}while(true);

return FULL_BASE_URL.'/img/bg/'.$img;
}


$str = get_random_image_ex();

if($str!== FALSE) {


$str = '<style type="text/css">
body {background-image:
' . $str . '
}
</style>
';
$vbulletin->templatecache['headinclude'] .= addcslashes($str, '\\"');

kh99
07-04-2012, 10:27 PM
Looks good, except the post is missing the last } .

stryka
07-04-2012, 10:45 PM
Darn... the entry is getting inserted into the HTML when i view source... but the page isn't taking the value and showing the image... i even tried deleting the "background" value in style template of admincp... hmmmm.... so close

kh99
07-04-2012, 10:56 PM
What does it look like when you view the html? Looking more closely at the function you posted, I don't see where the "background-image: url(" and the closing ")" come from - maybe those need to be added to the plugin code?

stryka
07-05-2012, 09:54 AM
yup.. that was it.. i usually dont get much help in this forum....

THanks for the help!

What does it look like when you view the html? Looking more closely at the function you posted, I don't see where the "background-image: url(" and the closing ")" come from - maybe those need to be added to the plugin code?