시간이 많이 소요되는 구현 (즉, 사용자 친화적 인 인터페이스)에 의존하지 않고 가능한 한 사용자 친화적 인 방식으로 (이미지 업데이트 앞면에서) 접근을 유지하는 열쇠는 모든 것을 쉽게 전달할 수있는 요소를 결정하는 것입니다 알고리즘으로 변경하십시오. 의심 할 여지없이 이러한 요소는 이미지 파일 (이름)입니다.
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에 대한 복잡한 호출 (일반적으로 문제가 있고 불안정한)을 피하고 내부에서 쉽게 수행 할 수있는 작업을 수행하는 것입니다. 즉 단순한 개념 증명 일뿐입니다.
Google 캘린더와 전혀 연결되지 않은 맞춤형 접근 방식을 고려해 보시겠습니까? – varocarbas
나는 확실히 –