From 53adb8bfafcda82ba5f2f69f1ab4a2939f573ea1 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 2 Apr 2016 19:34:01 +0100 Subject: [PATCH] Fix the unrecognised file type error image. --- build/index.php | 42 +++++++++++++++++++++++--------------- module_index.json | 2 +- modules/feature-upload.php | 42 +++++++++++++++++++++++--------------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/build/index.php b/build/index.php index c01714f..c104c03 100644 --- a/build/index.php +++ b/build/index.php @@ -2432,9 +2432,11 @@ register_module([ if(isset($_GET["type"]) and in_array($_GET["type"], [ "image/png", "image/jpeg", "image/webp" ])) $output_mime = $_GET["type"]; + $preview_image = false; switch(substr($mime_type, 0, strpos($mime_type, "/"))) { case "image": + // Read in the image $image = false; switch($mime_type) { @@ -2456,31 +2458,37 @@ register_module([ break; } + // Get the size of the image for later $raw_width = imagesx($image); $raw_height = imagesy($image); + // Resize the image $preview_image = resize_image($image, $target_size); - header("content-type: $output_mime"); - switch($output_mime) - { - case "image/jpeg": - imagejpeg($preview_image); - break; - case "image/png": - imagepng($preview_image); - break; - default: - case "image/webp": - imagewebp($preview_image); - break; - } - imagedestroy($preview_image); + // Delete the temporary image. + imagedestroy($image); break; default: http_response_code(501); - exit("Unrecognised file type."); + $preview_image = errorimage("Unrecognised file type '$mime_type'."); } + + // Send the completed preview image to the user + header("content-type: $output_mime"); + switch($output_mime) + { + case "image/jpeg": + imagejpeg($preview_image); + break; + case "image/png": + imagepng($preview_image); + break; + default: + case "image/webp": + imagewebp($preview_image); + break; + } + imagedestroy($preview_image); }); page_renderer::register_part_preprocessor(function(&$parts) { @@ -2563,7 +2571,7 @@ function errorimage($text) $image = imagecreatetruecolor($width, $height); imagefill($image, 0, 0, imagecolorallocate($image, 238, 232, 242)); // Set the background to #eee8f2 $fontwidth = imagefontwidth(3); - imagetext($image, 3, + imagestring($image, 3, ($width / 2) - (($fontwidth * strlen($text)) / 2), ($height / 2) - (imagefontheight(3) / 2), $text, diff --git a/module_index.json b/module_index.json index 4ed9e16..1142aa4 100644 --- a/module_index.json +++ b/module_index.json @@ -68,7 +68,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": 1459607200, + "lastupdate": 1459621957, "optional": false }, { diff --git a/modules/feature-upload.php b/modules/feature-upload.php index 64acd65..e9e9c76 100644 --- a/modules/feature-upload.php +++ b/modules/feature-upload.php @@ -204,9 +204,11 @@ register_module([ if(isset($_GET["type"]) and in_array($_GET["type"], [ "image/png", "image/jpeg", "image/webp" ])) $output_mime = $_GET["type"]; + $preview_image = false; switch(substr($mime_type, 0, strpos($mime_type, "/"))) { case "image": + // Read in the image $image = false; switch($mime_type) { @@ -228,31 +230,37 @@ register_module([ break; } + // Get the size of the image for later $raw_width = imagesx($image); $raw_height = imagesy($image); + // Resize the image $preview_image = resize_image($image, $target_size); - header("content-type: $output_mime"); - switch($output_mime) - { - case "image/jpeg": - imagejpeg($preview_image); - break; - case "image/png": - imagepng($preview_image); - break; - default: - case "image/webp": - imagewebp($preview_image); - break; - } - imagedestroy($preview_image); + // Delete the temporary image. + imagedestroy($image); break; default: http_response_code(501); - exit("Unrecognised file type."); + $preview_image = errorimage("Unrecognised file type '$mime_type'."); } + + // Send the completed preview image to the user + header("content-type: $output_mime"); + switch($output_mime) + { + case "image/jpeg": + imagejpeg($preview_image); + break; + case "image/png": + imagepng($preview_image); + break; + default: + case "image/webp": + imagewebp($preview_image); + break; + } + imagedestroy($preview_image); }); page_renderer::register_part_preprocessor(function(&$parts) { @@ -335,7 +343,7 @@ function errorimage($text) $image = imagecreatetruecolor($width, $height); imagefill($image, 0, 0, imagecolorallocate($image, 238, 232, 242)); // Set the background to #eee8f2 $fontwidth = imagefontwidth(3); - imagetext($image, 3, + imagestring($image, 3, ($width / 2) - (($fontwidth * strlen($text)) / 2), ($height / 2) - (imagefontheight(3) / 2), $text,