Steal Everything

Here you will find code I have used specifically on this site or on my client's sites.

Random background - drainpip.com - PHP

<?php
function randomBackground() {

$background = array(
0 => 'background1',
...
7 => 'background8');

srand ((double) microtime() * 100000);
$random_number = rand(0,count($background)-1);

$back = ($background[$random_number]);
return $back;
}

Creation of a function randomBackground that calls an array and sets the variable $back. I appended 1-6 in this array as the syntax is the same as 0. Below the array is a basic random number generator that creates the variable $random_number which will be 0-7 based on the $background array listed. We then set the value of variable $back by calling the array and asking for the array position we got from the random number generator. This seems like a roundabout way of doing things, but it will come in handy later.

This would be enough just to call 8 different backgrounds, but I also wanted the effect of looking at the same image from a different angle. For that, we will need to get a random position for the background styling as well.

function randomPos1() {

$posi1 = array(
0 => 'top',
1 => 'center');

srand ((double) microtime() * 100000);
$random_number = rand(0,count($posi1)-1);

$pos1 = ($posi1[$random_number]);
return $pos1;
}

function randomPos2() {

$posi2 = array(
0 => 'center',
1 => 'left',
2 => 'right',);

srand ((double) microtime() * 100000);
$random_number = rand(0,count($posi2)-1);

$pos2 = ($posi2[$random_number]);
return $pos2;
} ?>

Now we can set up an inline style for the <body> tag. The reason I wanted to include a randomized background-position is to utilize these gigantic images from NASA without cropping or resizing them. You might also notice I didn't include "2 => 'bottom'" into the $posi1 array. This is because the background will actually not show properly - try it and you will see what I'm talking about.

Now, let's call all of our functions into the inline style for the <body> tag.

<body style="background:url(http://www.drainpip.com/images/<?php echo randomBackground(); ?>.jpg) <?php echo randomPos1(); ?> <?php echo randomPos2(); ?> no-repeat;">

Thanks to the three functions, we can tell the body tag what background JPEG file to use as well as the X and Y positions of the background. You can use this for more than just gigantic images, but the effect is quite powerful using the Hubble images as an example. You can of course get even fancier by assigning random pixel placement for the background, but I felt this was more effective.