From: Alexander Sadleir Date: Sat, 17 Sep 2011 09:03:42 +0000 Subject: Change to simple white on black autoscaling header X-Git-Url: https://maxious.lambdacomplex.org/git/?p=photocalendar.git&a=commitdiff&h=aa93aeb5a1a53f83058b70013f61cc46a19a64c2 --- Change to simple white on black autoscaling header --- --- a/common.inc.php +++ b/common.inc.php @@ -2,202 +2,210 @@ include_once("config.inc.php"); include("php-calendar.lib.php"); + function include_header($title) { - ?> - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -
-
- -
-
- -
-
- - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+ +
+
+ +
+
+ + + + + + + + + ?> Upload a new day
Help/Instructions

- 0) { + return $results[0]; + } else + return false; +} + 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; -} -} + $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 getDateFromFilename($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); + $cacheFile = CACHE_DIR . $displayDate . ".png"; + if (file_exists($cacheFile)) { + return $cacheFile; + } else { + if ($fname = getFilenameForDate($displayDate, ".jpg")) { + $source_gd_image = imagecreatefromjpeg($fname); + } else if ($fname = getFilenameForDate($displayDate, ".png")) { + $source_gd_image = imagecreatefrompng($fname); + } else { + return false; + } + + if ($source_gd_image === false) { + return false; + } + $source_image_width = imagesx($source_gd_image); + $source_image_height = imagesy($source_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); + $textDayName = date("l", $date); + $sizeDayName = 24 + round(($source_image_height - 1000) / 1000) * 2; + $fontDayName = "./img/mplus-1p-medium.ttf"; + $bboxDayName = calculateTextBox($textDayName, $fontDayName, $sizeDayName); + + $textDate = date("jS F Y", $date); + $sizeDate = 24 + floor(($source_image_height - 1000) / 100); + $fontDate = "./img/mplus-1p-regular.ttf"; + $bboxDate = calculateTextBox($textDate, $fontDate, $sizeDate); + + $margin = 15; + +// Draw a black rectangle + imagefilledrectangle($source_gd_image, 0, 0, $source_image_width, $margin * 2 + $bboxDayName['height'], $black); // 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; -} + imagettftext($source_gd_image, $sizeDayName, 0, $margin + $bboxDayName['left'], $margin + $bboxDayName['top'], $white, $fontDayName, $textDayName); + imagettftext($source_gd_image, $sizeDate, 0, (($source_image_width - $bboxDayName['width']) / 2) + $bboxDayName['width'] - $bboxDate['left'], + $margin + $bboxDate['top'], $white, $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 = getDateFromFilename($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 + $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); -} ?> --- a/confirmUpload.php +++ b/confirmUpload.php @@ -1,46 +1,47 @@ upload page'; - $error = true; + echo 'No file was uploaded. You should start from the upload page'; + $error = true; } else { - $imageinfo = getimagesize($_FILES['userfile']['tmp_name']); - $source_image_type = $imageinfo['mime']; -$source_image_width = $imageinfo[0]; - $source_image_height = $imageinfo[1]; + $imageinfo = getimagesize($_FILES['userfile']['tmp_name']); + $source_image_type = $imageinfo['mime']; + $source_image_width = $imageinfo[0]; + $source_image_height = $imageinfo[1]; } -if($error == false && ($source_image_type != 'image/png' && $source_image_type != 'image/jpeg')) { +if ($error == false && ($source_image_type != 'image/png' && $source_image_type != 'image/jpeg')) { echo "Sorry, we only accept PNG and JPEG images. Your image was of type '$source_image_type'.
"; - $error = true; -} -if($error == false && ($source_image_width < MIN_IMAGE_SIZE || $source_image_height < MIN_IMAGE_SIZE)) { - echo "Sorry, we only accept images larger than ".MIN_IMAGE_SIZE." pixels. Your image was $source_image_width x $source_image_height pixels big.
"; - $error = true; -} - -if($error == false && ($source_image_width != $source_image_height)) { - echo "Sorry, we only accept images that are exactly square (the height is the same as the width). Your image was $source_image_width x $source_image_height pixels big.
"; - $error = true; -} + $error = true; +} +if ($error == false && ($source_image_width < MIN_IMAGE_SIZE || $source_image_height < MIN_IMAGE_SIZE || $source_image_width != $source_image_height)) { + echo "Sorry, your image wasn't big enough. In order to ensure that John's calendar looks super-beautiful, we only accept images that are 1000 px or larger and are squares. Your image was $source_image_width x $source_image_height pixels big.
"; + $error = true; +} + $hash = md5_file($_FILES['userfile']['tmp_name']); +if ($error == false && sizeof(glob(DATA_DIR."*".$hash."*")) > 0) { + echo "Sorry, we already have an image identical to this one.
"; + $error = true; +} + if (!$error) { - $fileExtension = ($source_image_type == 'image/png' ? ".png" : ".jpg"); - $fileDate = getNextAvailableDate(); - echo "Uploaded file meets all necessary requirements, next available date is $fileDate
"; - $uploaddir = '/var/spool/uploads/'; # Outside of web root - $uploadfile = DATA_DIR . $fileDate . $fileExtension; - if (file_exists($uploadfile)) { - echo "Oh no! A file for $fileDate already exists! Please retry in a moment
"; - } else { - if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { - echo "File was successfully uploaded.\n
"; - } else { - echo "File uploading failed.\n
"; - } - } + $fileExtension = ($source_image_type == 'image/png' ? ".png" : ".jpg"); + $fileDate = getNextAvailableDate(); + //echo "Uploaded file meets all necessary requirements, next available date is $fileDate
"; + $uploadfile = DATA_DIR . $fileDate . ".".$hash. $fileExtension; + if (file_exists($uploadfile)) { + echo "Oh no! A file for $fileDate already exists! Please retry in a moment
"; + } else { + if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { + echo "Yay! Your image has been added as $fileDate. Click here to go back to the site.\n
"; + } else { + echo "File uploading failed.\n
"; + } + } } include_footer(); ?> --- a/readme.txt +++ b/readme.txt @@ -2,7 +2,7 @@ Minimum Requirements PHP4 with gd2 library enabled - + If it's not installed or not enabled, you will get a "Fatal error: Call to undefined function imagecreatefromjpeg() in common.inc.php on line 88" error when viewing images Install: Unzip onto a webserver and you're done! --- a/upload.php +++ b/upload.php @@ -4,7 +4,7 @@ ?>
-Select the file to upload: +Select the file to upload: