Add image upload and image watermarking
[photocalendar.git] / common.inc.php
blob:a/common.inc.php -> blob:b/common.inc.php
--- a/common.inc.php
+++ b/common.inc.php
@@ -1,4 +1,7 @@
 <?php

+include_once("config.inc.php");

+

+include("php-calendar.lib.php");

 function include_header($title) {

 		?>

 		<!doctype html>

@@ -39,33 +42,154 @@
 

     </footer>

   </div> <!--! end of #container -->

-

-

-  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

-  <script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>

-

-

-  <!-- scripts concatenated and minified via ant build script

-  <script defer src="js/plugins.js"></script>

-  <script defer src="js/script.js"></script>

- end scripts-->

-

-

-  <!--  <script> // Change UA-XXXXX-X to be your site's ID

-    window._gaq = [['_setAccount','UAXXXXXXXX1'],['_trackPageview'],['_trackPageLoadTime']];

-    Modernizr.load({

-      load: ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js'

-    });

-  </script>-->

-

-

-  <!--[if lt IE 7 ]>

-    <script src="//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js"></script>

-    <script>window.attachEvent('onload',function(){CFInstall.check({mode:'overlay'})})</script>

-  <![endif]-->

   

 </body>

 </html>

 	<?php

 }

+

+function include_sidebar() {

+  ?>

+    <a href="upload.php">Upload a new day</a><br>

+    <a href="help.php">Help/Instructions</a><br>

+    <hr>

+<?php

+foreach (getCalendarMonths() as $month) {

+    echo generate_calendar($month['year'], $month['month'], getCalendarDays($month['year'], $month['month']), 3);

+}

+}

+

+function getNextAvailableDate() {

+  if ($previousDate = getPreviousDate()) {

+    $nextDayTime = strtotime("+1 day",strtotime($previousDate));

+    if (date("m",$nextDayTime) == "08" and date("d",$nextDayTime) == "24") {

+      // skip the 24th of August

+      $nextDayTime = strtotime("+1 day",$nextDayTime);

+    } 

+    return date("Y-m-d",$nextDayTime);

+  } else {

+    return START_DATE;

+  }

+}

+function getPreviousDate() {

+ $datedFiles = scandir(DATA_DIR); // sorted descendingly by default

+ if (sizeof($datedFiles) > 2) {// always at least 2 even for an empty folder because of ./ and ../

+  return removeImageFileExtensions($datedFiles[sizeof($datedFiles)-1]);

+} else {

+  return false;

+}

+}

+function getPhoto($displayDate) {

+  $cacheFile = CACHE_DIR.$displayDate.".png";

+  if (file_exists($cacheFile)) {

+    return $cacheFile;

+  } else {

+    if (file_exists(DATA_DIR.$displayDate.".jpg")) {

+        $source_gd_image = imagecreatefromjpeg( DATA_DIR.$displayDate.".jpg");

+      

+    } else if (file_exists(DATA_DIR.$displayDate.".png")) {

+              $source_gd_image = imagecreatefrompng( DATA_DIR.$displayDate.".png" );

+   } else {

+     return false;

+   }

+   

+    if ( $source_gd_image === false )

+    {

+      return false;

+    }

+$source_image_width = imagesx($source_gd_image);

+ $source_image_height =  imagesy($source_gd_image);

+

+               $header_gd_image = imagecreatefrompng( "img/header.png" );

+$header_image_width = imagesx($header_gd_image);

+ $header_image_height =  imagesy($header_gd_image);

+

+ $white = imagecolorallocate($source_gd_image, 255, 255, 255);

+  $black = imagecolorallocate($source_gd_image, 0, 0, 0);

+  function calculateTextBox($text,$fontFile,$fontSize) { 

+    /************ 

+    simple function that calculates the *exact* bounding box (single pixel precision). 

+    The function returns an associative array with these keys: 

+    left, top:  coordinates you will pass to imagettftext 

+    width, height: dimension of the image you have to create 

+    *************/ 

+    $rect = imagettfbbox($fontSize,0,$fontFile,$text); 

+    $minX = min(array($rect[0],$rect[2],$rect[4],$rect[6])); 

+    $maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6])); 

+    $minY = min(array($rect[1],$rect[3],$rect[5],$rect[7])); 

+    $maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7])); 

+    

+    return array( 

+     "left"   => abs($minX) - 1, 

+     "top"    => abs($minY) - 1, 

+     "width"  => $maxX - $minX, 

+     "height" => $maxY - $minY, 

+     "box"    => $rect 

+    ); 

+} 

+$date = strtotime($displayDate);

+// First we create our bounding box for the first text

+$textDayName = date("l",$date);

+$sizeDayName = 18;

+$fontDayName = "./img/mplus-1p-medium.ttf";

+$bboxDayName = calculateTextBox($textDayName,$fontDayName,$sizeDayName);

+

+$textDate = date("jS F Y",$date);

+$sizeDate = 14;

+$fontDate = "./img/mplus-1p-regular.ttf";

+$bboxDate = calculateTextBox($textDate,$fontDate,$sizeDate);

+

+$margin = 15;

+$maxX = $header_image_width + $margin*2 + max($bboxDayName['width'],$bboxDate['width']) + $margin*2;

+$maxY = max($header_image_height + $margin*2 , ($bboxDayName['height']+$margin+$bboxDate['height']));

+

+// Draw a white rectangle

+imagefilledrectangle($source_gd_image, 0, 0, $maxX, $maxY, $white);

+

+imagecopy($source_gd_image, $header_gd_image,$margin,$margin,0,0,$header_image_width,$header_image_height);

+

+// Write it

+imagettftext($source_gd_image, $sizeDayName, 0, $header_image_width+$margin*2+$bboxDayName['left'], $margin+$bboxDayName['top'], $black, $fontDayName, $textDayName);

+imagettftext($source_gd_image, $sizeDate, 0, $header_image_width+$margin*2+$bboxDate['left'], $margin+$bboxDayName['height']+$margin+$bboxDate['top'], $black, $fontDate, $textDate);

+

+    imagepng( $source_gd_image, $cacheFile, 9 );

+    imagedestroy( $source_gd_image );

+    return $cacheFile;

+  }

+}

+function getCalendarDays($year,$month) {

+  $result = Array();

+  if ($handle = opendir(DATA_DIR)) {

+    while (false !== ($file = readdir($handle))) {

+        if ($file != "." && $file != ".." && startsWith($file,"$year-$month")) {

+          $parts = explode("-",$file);

+          $day = removeImageFileExtensions($parts[2]);

+           $result[$day]=Array("index.php?date=$year-$month-$day",'linked-day');

+        }

+      }

+  }

+  ksort($result);

+  return $result;

+}

+function getCalendarMonths() {

+  $months = Array();

+  if ($handle = opendir(DATA_DIR)) {

+    while (false !== ($file = readdir($handle))) {

+       if ($file != "." && $file != "..") {

+          $parts = explode("-",$file);

+          $months[$parts[0].$parts[1]]=Array("month"=>$parts[1],"year"=>$parts[0]);

+        }

+      }

+   }

+  return $months;

+}

+function startsWith($haystack, $needle)

+{

+  // source: http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions

+    $length = strlen($needle);

+    return (substr($haystack, 0, $length) === $needle);

+}

+function removeImageFileExtensions($filename) {

+  return  str_replace(Array(".png",".jpg"),"",$filename);

+}

 ?>