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

Php 随机背景图像在chrome中不起作用?,php,background-process,Php,Background Process,我正在尝试随机制作背景 background:url(images/background/background-image.php) no-repeat fixed; background-size: 100%; 它适用于firefox、safari和IE,但不适用于chrome background-image.php <?php $folder = '.'; $extList = array(); $extList['gif'] = 'image

我正在尝试随机制作背景

background:url(images/background/background-image.php) no-repeat fixed;
background-size: 100%; 
它适用于firefox、safari和IE,但不适用于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);
        }
    }                                                        
?>

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

background:url(<?=$bgimage?>) no-repeat fixed;
并让PHP脚本输出变量$bgimage

注意速记

<?=$bgimage?> 
就是说

<?php echo $bgimage; ?>

而不是生成生成实际图像文件的脚本。我会使脚本引用成为指向该文件的链接。例如,如果目录中的图像是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%;

还请注意,您没有在背景中包含单个引号:url“…”;CSS样式。如果我正确地阅读了您的代码,您会发现您的问题可能过于复杂。

这是文件最初的样子吗?我想,将php文件的标题设置为image/jpeg。它在其他浏览器中工作吗?不,工作正常-我总是使用这个速记来回送变量。所有这些都是等效的,或者我的投票被锁定,除非答案根据stackoverflow编辑。啊,抱歉,我不知道你不能编辑它。事实上,你不需要在CSS的URL字符串周围加引号@奥奇鲁这有帮助吗?你看到我的答案了吗,还是完全浪费了我的时间?