根据Google日历计划更改img标记

时间:2013-07-20 16:06:43

标签: php javascript image api google-calendar-api

我有一个网站,我们有一个播放器(点击以下链接访问它) FillyRadio Player

有一个图像位于背景和波形画布前面。

根据Google日程安排,我需要将此图片更改为各种演示者的不同图片(每次我们更改计划时,都会比使用代码更容易)。

我们有什么方法可以根据Google日历更改此img src代码?

注意:我对使用API​​非常陌生,所以我会很感激愚蠢的答案!

我对这种方法并不挑剔,只要它适用于Chrome和Firefox。

1 个答案:

答案 0 :(得分:0)

保持方法尽可能方便用户(在图像更新方面)而不必依赖耗时的实现(即用户友好界面)的关键是确定可以轻松传达任何变化的元素算法。毫无疑问,这些元素是图像文件(它们的名称)。我很快就提出了一个命名约定来提供你想要的东西(虽然预计会有很大的改进):

07-23-2013_2.jpg (or .png or any pic extension)
3_1.jpg 

所有名称都包含一个“_”,它将第一个元素(“值”)与第二个元素(“持续时间”)分开。该值可以是特定日期(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的复杂调用,以执行可以在内部轻松完成的操作;也就是说,仅仅是一个概念证据。