0

I'm trying to make random background.

background:url(images/background/background-image.php) no-repeat fixed;
background-size: 100%; 

it works in firefox, safari and IE, but it doesn't work in chrome.

background-image.php

<?php

    $folder = '.';
    $extList = array();     
    $extList['gif'] = 'image/gif';  
    $extList['jpg'] = 'image/jpeg';     
    $extList['jpeg'] = 'image/jpeg';    
    $extList['png'] = 'image/png';  

    $img = null;

    if (substr($folder,-1) != '/') { 
        $folder = $folder.'/'; 
    }

    if (isset($_GET['img'])) {  
        $imageInfo = pathinfo($_GET['img']);    
        if ( isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) && file_exists( $folder.$imageInfo['basename'] ) ) {        
            $img = $folder.$imageInfo['basename'];  
        } 
    } else {    
        $fileList = array();    
        $handle = opendir($folder);     
        while ( false !== ( $file = readdir($handle) ) ) {      
            $file_info = pathinfo($file);       
            if (isset( $extList[ strtolower( $file_info['extension'] ) ] ) ) {                    
                $fileList[] = $file;        
            }   
        }   
        closedir($handle);
        if (count($fileList) > 0) {         
            $imageNumber = time() % count($fileList);       
            $img = $folder.$fileList[$imageNumber];     
        } 
    }

    if ($img!=null) {   
        $imageInfo = pathinfo($img);    
        $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];    
        header ($contentType);  
        readfile($img); 
    } else {    
        if ( function_exists('imagecreate') ) {         
            header ("Content-type: image/png");         
            $im = @imagecreate (100, 100) or die ("Cannot initialize new GD image stream");         
            $background_color = imagecolorallocate ($im, 255, 255, 255); 
            $text_color = imagecolorallocate ($im, 0,0,0);
            imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color); 
            imagepng ($im); 
            imagedestroy($im);
        }
    }                                                        
?>
SomeKittens
  • 38,868
  • 19
  • 114
  • 143

2 Answers2

0

Instead of making a script that generates the actual image file. I would make the script reference a link to the file. For example if in directory images their were 10 random images I would use PHP's rand function with scandir.

NOTE: I have not tested the code below. It serves as a "rough idea".

<?php
    $website_path = "http://www.example.com/".$GET['imgpath'];
    $list = scandir($_GET['imgpath']);
    if(count($list)>0){ //yes images in directory
        $random = rand(0,count($list));
        $random_path = $list[$random];
    }else               //no images in directory
        $random_path = "/images/error.php";
    $random_path = $website_path."/".$random_path;
    echo "background:url('$random_path');";
?>
background-size:100%;

Also note that you did not include the single quotations around the background:url('...'); CSS style. If I read your code right you can see that you may have over complicated matters.

Glenn Dayton
  • 1,410
  • 2
  • 20
  • 38
  • Actually, you don't need quotations around the URL string for CSS. http://stackoverflow.com/questions/2168855/css-url-are-quotes-needed – zenkaty Aug 21 '12 at 08:01
  • @Ochiroo Was this helpful? Did you even see my answer, or was it a complete waste of my time? – Glenn Dayton Aug 21 '12 at 21:31
-1

Can you put the CSS in a PHP file, and use a variable to list the background image path?

background:url(<?=$bgimage?>) no-repeat fixed;

And have your PHP script output the variable $bgimage

Note that the shorthand

<?=$bgimage?> 

is the same as saying

<?php echo $bgimage; ?>
zenkaty
  • 1,538
  • 9
  • 11