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);
        }
    }                                                        
?>
4

2 回答 2

0

而不是制作生成实际图像文件的脚本。我会让脚本引用文件的链接。例如,如果在目录中images它们是 10 个随机图像,我将使用 PHP 的rand函数与scandir.

注意:我没有测试下面的代码。它是一个“粗略的想法”。

<?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%;

另请注意,您没有在background:url('...');CSS 样式周围包含单引号。如果我正确阅读了您的代码,您会发现您可能遇到了复杂的问题。

于 2012-08-21T07:18:13.457 回答
-1

你能把CSS放在一个PHP文件中,并使用一个变量来列出背景图像的路径吗?

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

并让您的 PHP 脚本输出变量 $bgimage

请注意,简写

<?=$bgimage?> 

和说的一样

<?php echo $bgimage; ?>
于 2012-08-21T07:01:24.777 回答