PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jun 19, 2018 1:23 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Mon Aug 22, 2016 7:01 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4417
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
<style>
.home-banner
{
position: relative;
    /* The image used */
    background-image: url('../images/banner-home.jpg');
    /* Full height */
    height: 83%;
    /* Create the parallax scrolling effect */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
</style>
 


The idea is that each time the page loads, it can load a different image, maybe from a set of three.
I've seen how to do when the image is directly in the DIV, but not as a background.

And coding the background-image code into the HTML is bad-form. So is there a good way to do it?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Mon Aug 22, 2016 8:04 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Either
a) Use a random path in the CSS itself.
b) Point the CSS to a PHP script and have it randomize the image.

The latter is probably your best bet.
Code:
background-image: url('../images/banner-random.php');

Syntax: [ Download ] [ Hide ]
<?php

$images = array(
        'banner-home.jpg',
        ...
);
$image = $images[array_rand($images)];

$filename = __DIR__ . '/' . $image;

header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($filename));
readfile($filename);
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group