我有一个网站,我们有一个玩家(点击下面的链接访问它)FillyRadio播放器
有一个图像位于背景和波形画布前面。
我需要根据谷歌日历时间表将这张图片更改为不同主持人的不同图片(这比每次更改时间表时都用代码放屁更容易)。
有没有什么方法可以根据谷歌日历更改这个img src标记?
注意:我是API的新手,所以如果能给我一个愚蠢的回答将不胜感激!
我对这个方法并不挑剔,只要它能与Chrome和Firefox配合使用。
保持该方法尽可能用户友好(在图像更新方面)而不必依赖耗时的实现(即用户友好的界面)的关键是确定可以轻松传达算法任何更改的元素。毫无疑问,这些元素就是图像文件(它们的名称)。我很快就想出了一个命名约定来满足您的需求(尽管您预计会对其进行高度改进):
07-23-2013_2.jpg (or .png or any pic extension)
3_1.jpg
所有的名称都包含一个"_",它将第一个元素("value")与第二个元素(("duration")分隔开。该值可以是某个日期(mm-dd-yyyy)或计数器:某个日期意味着该图像必须在这一天使用,无论发生什么;计数器是默认行为:如果没有给定日期的图片,算法将通过关注该计数器来考虑下一个。持续时间是指给定图片预计出现的天数。
为了正确解释我的观点,我为前面提到的命名结构创建了一个代码说明。但是,请正确理解这个答案的意图:这只是向您展示一个替代解决方案,因此我希望您对代码进行大量更改/改进。
<?php
$goAhead = true;
$today = strtotime(date('Y') . "-" . date('m') . "-" . date('d'));
$lastValue = 0;
//Checking the last stored date and value
if(file_exists ("./config.ini"))
{
$lines = file("./config.ini");
if(count($lines) >= 2)
{
if(strstr($lines[0],'-'))
{
$temp = explode ('-' , $lines[0] );
$goAhead = false;
if(isDateGreaterEqual($temp, $today, false))
{
//The pic has to be changed no matter what
$lastValue = (int)$lines[1];
$goAhead = true;
}
}
}
}
$bestVal = $lastValue;
$bestDuration = 0;
$dirName = './images';
$selectedPic = "";
$bestPic = "";
foreach(glob($dirName . '/*.*') as $file)
{
if(strstr($file,'_') && strstr($file,'.'))
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
$fileNoExtension = basename($file, "." . $ext);
$temp = explode ('_' , $fileNoExtension );
$startVal = $temp[0]; //Date or number
$duration = $temp[1]; //Number of days the given pic will be up
if(strstr($startVal,'-'))
{
$temp = explode ('-' , $startVal );
if(isDateGreaterEqual($temp, $today, true))
{
//If the pic has today's date, the change has to be performed no matter what
updateConfig($duration, $lastValue);
$selectedPic = $file;
break;
}
}
else if($goAhead)
{
//Only in cases where the pic has to be updated
if((int)startVal < $bestVal)
{
$bestVal = (int)startVal;
$bestDuration = $duration;
$bestPic = $file;
}
}
}
}
if($bestPic != "")
{
$selectedPic = $bestPic;
updateConfig($bestDuration, $bestVal);
}
if($selectedPic != "")
{
//$selectedPic -> path of the new image
}
function isDateGreaterEqual($temp, $today, $justEqual)
{
$conditionMet = false;
if(count($temp) == 3)
{
if(checkdate((int)$temp[0], (int)$temp[1], (int)$temp[2]))
{
$stored_date = strtotime($temp[2] . "-" . $temp[0] . "-" . $temp[1]);
if((!$justEqual && $today >= $stored_date) || ($justEqual && $today == $stored_date))
{
//The pic has to be changed no matter what
$conditionMet = true;
}
}
}
return $conditionMet;
}
function updateConfig($duration, $curVal)
{
$finalDate = date('m-d-Y', strtotime("+" . $duration . " days"));
$file = fopen('./config.ini', 'w') or die("can't open file");
fwrite($file, $finalDate . "'r'n");
fwrite($file, $curVal);
fclose($file);
}
?>
此代码期望图片(具有如上所述的名称)存储在文件夹images
中。它将当前配置(显示给定图片的日期和给定计数器)存储在一个名为config.ini
的文件中。该代码的输出是$selectedPic
,它可能是空白的(没有图片更改)或包括新图片的文件名。此代码预计将定期调用(每天只调用一次就足够了)。
这个建议的全部目的是避免(通常是有问题和不稳定的)对外部API的复杂调用,以执行可以在内部轻松完成的操作;也就是说,仅仅是概念的证明。