我有一個文件夾中的圖像,我想隨機化圖像顯示爲完整背景圖像,同時顯示當前背景圖像的文本描述顯示?代碼如下所示。如何綁定或循環隨機背景圖像到文本
<?php
function displayBackground()
{
$dir = 'control/uploads/image/home_bg/';
$cnt = 0;
$bgArray= array();
/*if we can load the directory*/
if ($handle = opendir($dir)) {
/* Loop through the directory here */
while (false !== ($entry = readdir($handle))) {
$pathToFile = $dir.$entry;
if(is_file($pathToFile)) //if the files exists
{
//make sure the file is an image...there might be a better way to do this
if(getimagesize($pathToFile)!=FALSE)
{
//add it to the array
$bgArray[$cnt]= $pathToFile;
$cnt = $cnt+1;
}
}
}
//create a random number, then use the image whos key matches the number
$myRand = rand(0,($cnt-1));
$val = $bgArray[$myRand];
}
closedir($handle);
echo('"'.$val.'"');
}
?>
到目前爲止您嘗試過什麼?請閱讀[_我如何問一個好問題_](http://stackoverflow.com/help/how-to-ask) – Oka
我曾嘗試使用PHP來隨機化背景圖片,但不知道如何輸出文字描述爲當前圖像顯示。 –
然後顯示你已經嘗試了什麼,以及你卡在哪裏。沒有人可以幫助你看到他們看不到的代碼。 – Oka