mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-11-25 17:23:00 +00:00
Finish preview generator
This commit is contained in:
parent
f688757bb9
commit
5004544b25
4 changed files with 123 additions and 72 deletions
117
build/index.php
117
build/index.php
|
@ -186,6 +186,12 @@ $settings->upload_allowed_file_types = [
|
|||
"image/webp"
|
||||
];
|
||||
|
||||
// The default file type for previews. Defaults to image/webp. Webp is a new
|
||||
// image format that can cut image sizez down by ~20%, but may still have some
|
||||
// issues in certain browsers. Change this to image/png or image/jpeg if you
|
||||
// experience issues.
|
||||
$settings->preview_file_type = "image/webp";
|
||||
|
||||
// The location of a file that maps mime types onto file extensions and vice
|
||||
// versa. Used to generate the file extension for an uploaded file. Set to the
|
||||
// default location of the mime.types file on Linux. If you aren't using linux,
|
||||
|
@ -1363,33 +1369,7 @@ register_module([
|
|||
$filepath = $pageindex->{$env->page}->uploadedfilepath;
|
||||
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
||||
|
||||
switch(substr($mime_type, 0, strpos($mime_type, "/")))
|
||||
{
|
||||
case "image":
|
||||
$preview = false;
|
||||
switch($mime_type)
|
||||
{
|
||||
case "image/jpeg":
|
||||
$preview = imagecreatefromjpeg($filepath);
|
||||
break;
|
||||
case "image/gif":
|
||||
$preview = imagecreatefromgif($filepath);
|
||||
break;
|
||||
case "image/png":
|
||||
$preview = imagecreatefrompng($filepath);
|
||||
break;
|
||||
case "image/webp":
|
||||
$preview = imagecreatefromwebp($filepath);
|
||||
break;
|
||||
default:
|
||||
$preview = errorimage("Unsupported image type.");
|
||||
break;
|
||||
}
|
||||
|
||||
$raw_width = imagesx($preview);
|
||||
$raw_height = imagesy($preview);
|
||||
$aspect_ratio = $raw_width / $raw_height;
|
||||
|
||||
// Determine the target size of the image
|
||||
$target_size = 512;
|
||||
if(isset($_GET["size"]))
|
||||
$target_size = intval($_GET["size"]);
|
||||
|
@ -1398,23 +1378,58 @@ register_module([
|
|||
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.
|
||||
// Determine the output file type
|
||||
$output_mime = $settings->preview_file_type;
|
||||
if(isset($_GET["type"]) and in_array($_GET["type"], [ "image/png", "image/jpeg", "image/webp" ]))
|
||||
$output_mime = $_GET["type"];
|
||||
|
||||
switch(substr($mime_type, 0, strpos($mime_type, "/")))
|
||||
{
|
||||
case "image":
|
||||
$image = false;
|
||||
switch($mime_type)
|
||||
{
|
||||
case "image/jpeg":
|
||||
$image = imagecreatefromjpeg($filepath);
|
||||
break;
|
||||
case "image/gif":
|
||||
$image = imagecreatefromgif($filepath);
|
||||
break;
|
||||
case "image/png":
|
||||
$image = imagecreatefrompng($filepath);
|
||||
break;
|
||||
case "image/webp":
|
||||
$image = imagecreatefromwebp($filepath);
|
||||
break;
|
||||
default:
|
||||
$image = errorimage("Unsupported image type.");
|
||||
break;
|
||||
}
|
||||
|
||||
$raw_width = imagesx($image);
|
||||
$raw_height = imagesy($image);
|
||||
|
||||
$image = resize_image($image, $target_size);
|
||||
|
||||
header("content-type: $output_mime");
|
||||
switch($output_mime)
|
||||
{
|
||||
case "image/jpeg":
|
||||
imagejpeg($image);
|
||||
break;
|
||||
case "image/png":
|
||||
imagepng($image);
|
||||
break;
|
||||
default:
|
||||
case "image/webp":
|
||||
imagewebp($image);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
http_response_code(501);
|
||||
exit("Unrecognised file type.");
|
||||
}
|
||||
|
||||
// todo render a preview here
|
||||
|
@ -1482,6 +1497,24 @@ function errorimage($text)
|
|||
return $image;
|
||||
}
|
||||
|
||||
function resize_image($image, $size)
|
||||
{
|
||||
$cur_width = imagesx($image);
|
||||
$cur_height = imagesy($image);
|
||||
|
||||
if($cur_width < $size and $cur_height < $size)
|
||||
return $image;
|
||||
|
||||
$width_ratio = $size / $cur_width;
|
||||
$height_ratio = $size / $cur_height;
|
||||
$ratio = min($width_ratio, $height_ratio);
|
||||
|
||||
$new_height = $cur_height * $ratio;
|
||||
$new_width = $cur_width * $ratio;
|
||||
|
||||
return imagescale($image, $new_width, $new_height);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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": 1445630469,
|
||||
"lastupdate": 1445677429,
|
||||
"optional": false
|
||||
},
|
||||
{
|
||||
|
|
|
@ -138,33 +138,6 @@ register_module([
|
|||
$filepath = $pageindex->{$env->page}->uploadedfilepath;
|
||||
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
||||
|
||||
switch(substr($mime_type, 0, strpos($mime_type, "/")))
|
||||
{
|
||||
case "image":
|
||||
$preview = false;
|
||||
switch($mime_type)
|
||||
{
|
||||
case "image/jpeg":
|
||||
$preview = imagecreatefromjpeg($filepath);
|
||||
break;
|
||||
case "image/gif":
|
||||
$preview = imagecreatefromgif($filepath);
|
||||
break;
|
||||
case "image/png":
|
||||
$preview = imagecreatefrompng($filepath);
|
||||
break;
|
||||
case "image/webp":
|
||||
$preview = imagecreatefromwebp($filepath);
|
||||
break;
|
||||
default:
|
||||
$preview = errorimage("Unsupported image type.");
|
||||
break;
|
||||
}
|
||||
|
||||
$raw_width = imagesx($preview);
|
||||
$raw_height = imagesy($preview);
|
||||
$aspect_ratio = $raw_width / $raw_height;
|
||||
|
||||
// Determine the target size of the image
|
||||
$target_size = 512;
|
||||
if(isset($_GET["size"]))
|
||||
|
@ -174,32 +147,53 @@ register_module([
|
|||
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)
|
||||
// Determine the output file type
|
||||
$output_mime = $settings->preview_file_type;
|
||||
if(isset($_GET["type"]) and in_array($_GET["type"], [ "image/png", "image/jpeg", "image/webp" ]))
|
||||
$output_mime = $_GET["type"];
|
||||
|
||||
switch(substr($mime_type, 0, strpos($mime_type, "/")))
|
||||
{
|
||||
if($raw_width > $raw_height)
|
||||
case "image":
|
||||
$image = false;
|
||||
switch($mime_type)
|
||||
{
|
||||
$preview_width = $target_size;
|
||||
$preview_height = $target_size * $aspect_ratio;
|
||||
}
|
||||
else
|
||||
{
|
||||
$preview_height = $target_size;
|
||||
$preview_width = $target_size * $aspect_ratio;
|
||||
}
|
||||
case "image/jpeg":
|
||||
$image = imagecreatefromjpeg($filepath);
|
||||
break;
|
||||
case "image/gif":
|
||||
$image = imagecreatefromgif($filepath);
|
||||
break;
|
||||
case "image/png":
|
||||
$image = imagecreatefrompng($filepath);
|
||||
break;
|
||||
case "image/webp":
|
||||
$image = imagecreatefromwebp($filepath);
|
||||
break;
|
||||
default:
|
||||
$image = errorimage("Unsupported image type.");
|
||||
break;
|
||||
}
|
||||
|
||||
$raw_width = imagesx($image);
|
||||
$raw_height = imagesy($image);
|
||||
|
||||
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.
|
||||
$image = resize_image($image, $target_size);
|
||||
|
||||
header("content-type: $output_mime");
|
||||
switch($output_mime)
|
||||
{
|
||||
case "image/jpeg":
|
||||
imagejpeg($image);
|
||||
break;
|
||||
case "image/png":
|
||||
imagepng($image);
|
||||
break;
|
||||
default:
|
||||
case "image/webp":
|
||||
imagewebp($image);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -272,4 +266,22 @@ function errorimage($text)
|
|||
return $image;
|
||||
}
|
||||
|
||||
function resize_image($image, $size)
|
||||
{
|
||||
$cur_width = imagesx($image);
|
||||
$cur_height = imagesy($image);
|
||||
|
||||
if($cur_width < $size and $cur_height < $size)
|
||||
return $image;
|
||||
|
||||
$width_ratio = $size / $cur_width;
|
||||
$height_ratio = $size / $cur_height;
|
||||
$ratio = min($width_ratio, $height_ratio);
|
||||
|
||||
$new_height = $cur_height * $ratio;
|
||||
$new_width = $cur_width * $ratio;
|
||||
|
||||
return imagescale($image, $new_width, $new_height);
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -183,6 +183,12 @@ $settings->upload_allowed_file_types = [
|
|||
"image/webp"
|
||||
];
|
||||
|
||||
// The default file type for previews. Defaults to image/webp. Webp is a new
|
||||
// image format that can cut image sizez down by ~20%, but may still have some
|
||||
// issues in certain browsers. Change this to image/png or image/jpeg if you
|
||||
// experience issues.
|
||||
$settings->preview_file_type = "image/webp";
|
||||
|
||||
// The location of a file that maps mime types onto file extensions and vice
|
||||
// versa. Used to generate the file extension for an uploaded file. Set to the
|
||||
// default location of the mime.types file on Linux. If you aren't using linux,
|
||||
|
|
Loading…
Reference in a new issue