From db81d17c5ea0485e8ab61bb3277fc90af6c46f58 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 23 Oct 2015 21:04:58 +0100
Subject: [PATCH] Continue work on aspect ratio of preview images

---
 build/index.php            | 31 +++++++++++++++++++------
 module_index.json          |  2 +-
 modules/feature-upload.php | 46 ++++++++++++++++++++++++++++++++------
 3 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/build/index.php b/build/index.php
index e97d46a..71df3b7 100644
--- a/build/index.php
+++ b/build/index.php
@@ -1386,14 +1386,31 @@ register_module([
 							break;
 					}
 					
-					$aspect_ratio = imagesx($preview) / imagesy($preview);
+					$raw_width = imagesx($preview);
+					$raw_height = imagesy($preview);
+					$aspect_ratio = $raw_width / $raw_height;
 					
-					$target_width = intval($_GET["size"]);
-					if($target_width < $settings->min_preview_size)
-						$target_width = $settings->min_preview_size;
-					if($target_width > $settings->max_preview_size)
-						$target_width = $settings->max_preview_size;
-					$target_height = $target_width;
+					$target_size = 512;
+					if(isset($_GET["size"]))
+						$target_size = intval($_GET["size"]);
+					if($target_size < $settings->min_preview_size)
+						$target_size = $settings->min_preview_size;
+					if($target_size > $settings->max_preview_size)
+						$target_size = $settings->max_preview_size;
+					
+					if($raw_width > $raw_height)
+					{
+						$preview_width = $target_size;
+						$preview_height = $target_size * $aspect_ratio;
+					}
+					else
+					{
+						$preview_height = $target_size;
+						$preview_width = $target_size * $aspect_ratio;
+					}
+					header("content-type: text/plain");
+					echo("raw: $raw_width x $raw_height\n");
+					echo("resized: $preview_width x $preview_height\n");
 					
 					// Todo Scale image to fit inside size.
 					
diff --git a/module_index.json b/module_index.json
index 4d3b556..22c96c9 100644
--- a/module_index.json
+++ b/module_index.json
@@ -50,7 +50,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
         "id": "feature-upload",
-        "lastupdate": 1445504176,
+        "lastupdate": 1445630469,
         "optional": false
     },
     {
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index c6f4f39..abc6e21 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -161,18 +161,50 @@ register_module([
 							break;
 					}
 					
-					$aspect_ratio = imagesx($preview) / imagesy($preview);
+					$raw_width = imagesx($preview);
+					$raw_height = imagesy($preview);
+					$aspect_ratio = $raw_width / $raw_height;
 					
-					$target_width = intval($_GET["size"]);
-					if($target_width < $settings->min_preview_size)
-						$target_width = $settings->min_preview_size;
-					if($target_width > $settings->max_preview_size)
-						$target_width = $settings->max_preview_size;
-					$target_height = $target_width;
+					// Determine the target size of the image
+					$target_size = 512;
+					if(isset($_GET["size"]))
+						$target_size = intval($_GET["size"]);
+					if($target_size < $settings->min_preview_size)
+						$target_size = $settings->min_preview_size;
+					if($target_size > $settings->max_preview_size)
+						$target_size = $settings->max_preview_size;
+					
+					// Set the preview size equal to the original image dimensions
+					$preview_width = $raw_width;
+					$preview_height = $raw_height;
+					// If the original image doesn't fit inside the box, resize
+					// it's dimensions, preserving aspect ratio
+					if($raw_width > $target_size || $raw_height > $target_size)
+					{
+						if($raw_width > $raw_height)
+						{
+							$preview_width = $target_size;
+							$preview_height = $target_size * $aspect_ratio;
+						}
+						else
+						{
+							$preview_height = $target_size;
+							$preview_width = $target_size * $aspect_ratio;
+						}
+					}
+					
+					
+					header("content-type: text/plain");
+					echo("raw: $raw_width x $raw_height\n");
+					echo("resized: $preview_width x $preview_height\n");
 					
 					// Todo Scale image to fit inside size.
 					
 					break;
+				
+				default:
+					http_response_code(501);
+					exit("Unrecognised file type.");
 			}
 			
 			// todo render a preview here
