随机背景图片在chrome中不起作用?

时间:2012-08-21 04:31:33

标签: php background-process

我正在尝试制作随机背景。

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

它适用于firefox,safari和IE,但它在chrome中不起作用。

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

2 个答案:

答案 0 :(得分:0)

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

注意:我没有测试下面的代码。它是一个粗略的想法&#34;。

<?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样式周围添加单引号。如果我正确阅读您的代码,您可以看到您可能遇到过复杂的问题。

答案 1 :(得分:-1)

您可以将CSS放在PHP文件中,并使用变量列出背景图片路径吗?

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

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

注意简写

<?=$bgimage?> 

与说

相同
<?php echo $bgimage; ?>