শনিবার, ১৪ মার্চ, ২০০৯

JavaScript Image Resize on the fly....

Re-size your images of a container (ex: table, div etc) to a maximum width maintaining aspect ratio.

/* Code - Start */

function fixImgs(containerId, maxW) {
var pix=document.getElementById(containerId).getElementsByTagName('img');
for (i=0; i maxW) {
f=1-((w - maxW) / w);
pix[i].width=w * f;
pix[i].height=h * f;
}
}
}

/* Code - End */

add onLoad="fixImgs(containerId, maxWidth);" in the body tag.

শুক্রবার, ১৩ মার্চ, ২০০৯

Hit counter in php without database

function calculate_hits($timeFile, $dayFile){
// calculate current time
$currentTime = time();
// get content of timeFile
$fileData = file_get_contents($timeFile);
// if this is the first hit ever
if($fileData == ""){
$fileData = $currentTime;
file_put_contents($timeFile, $fileData);
return "1|1|1";
}
// get all times in array
$timeContent = explode("|", $fileData);
// calculate total hits of this file
$totalHits = count($timeContent);
// find out the last hit time
$lastHit = $timeContent[$totalHits - 1];

$today = date('d-m-Y', $currentTime);
$todayArray = explode("-", $today);
$todayInTime = mktime(0,0,0,$todayArray[1],$todayArray[0],$todayArray[2]);

if(strcmp(date('d-m-Y',$lastHit),date('d-m-Y')) != 0){ // if lastHit date is not today
$archiveTimeIndicator = $todayInTime - 86400;
$to_remove_string = "";
$i = $break_flag = 0;
for(;$i<$totalHits;$i++){
if($archiveTimeIndicator < $timeContent[$i]){
$break_flag = 1;
break;
}
$to_remove_string .= $timeContent[$i]."|";
}

if($break_flag){
$fileData = str_replace($to_remove_string, "", $fileData);
}else{
$fileData = "";
}
file_put_contents($dayFile, date('d-m-Y', $archiveTimeIndicator - 86400)."|".$i."|".file_get_contents($dayFile));
}

// find out the time of 24 hours ago
$timeBefore24hrs = $currentTime - 86400;
// find out hits for last 24 hours
$hits24hrs = 1;
while($timeBefore24hrs <= $timeContent[$totalHits - $hits24hrs]){
$hits24hrs++;
}

// update timeFile
if($fileData == ""){
$fileData = $currentTime;
}else{
$fileData .= "|".$currentTime;
}

file_put_contents($timeFile, $fileData);

$currentTotalHits = $totalHits + 1;

// get content of dayFile
$fileData = file_get_contents($dayFile);
// get all entries in array
$fileContent = explode("|", $fileData);
// calculate number of entries of this file
$totalEntries = count($fileContent);

if($totalEntries < 2){ // no entry in the dayFile yet
return $currentTotalHits."|".$currentTotalHits."|".$currentTotalHits;
}

$timeBefore31days = $todayInTime - 31*86400;
$timeBeforeOneYear = $todayInTime - 365*86400;

$hitsUpto31days = $histUptoOneYear = $currentTotalHits;

for($i=0; $i<$totalEntries; $i+=2){
$dateFromFile = explode("-", $fileContent[$i]);
$timeFromFile = mktime(0, 0, 0, $dateFromFile[1], $dateFromFile[0], $dateFromFile[2]);
$hitsFromFile = $fileContent[$i + 1];

if($timeFromFile >= $timeBefore31days){
$hitsUpto31days += $hitsFromFile;
$histUptoOneYear += $hitsFromFile;
}else if($timeFromFile >= $timeBeforeOneYear){
$histUptoOneYear += $hitsFromFile;
}else{
break;
}
}

return $hits24hrs."|".$hitsUpto31days."|".$histUptoOneYear;
}