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
|
@ -186,6 +186,12 @@ $settings->upload_allowed_file_types = [
|
||||||
"image/webp"
|
"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
|
// 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
|
// 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,
|
// default location of the mime.types file on Linux. If you aren't using linux,
|
||||||
|
@ -1363,58 +1369,67 @@ register_module([
|
||||||
$filepath = $pageindex->{$env->page}->uploadedfilepath;
|
$filepath = $pageindex->{$env->page}->uploadedfilepath;
|
||||||
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// 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, "/")))
|
switch(substr($mime_type, 0, strpos($mime_type, "/")))
|
||||||
{
|
{
|
||||||
case "image":
|
case "image":
|
||||||
$preview = false;
|
$image = false;
|
||||||
switch($mime_type)
|
switch($mime_type)
|
||||||
{
|
{
|
||||||
case "image/jpeg":
|
case "image/jpeg":
|
||||||
$preview = imagecreatefromjpeg($filepath);
|
$image = imagecreatefromjpeg($filepath);
|
||||||
break;
|
break;
|
||||||
case "image/gif":
|
case "image/gif":
|
||||||
$preview = imagecreatefromgif($filepath);
|
$image = imagecreatefromgif($filepath);
|
||||||
break;
|
break;
|
||||||
case "image/png":
|
case "image/png":
|
||||||
$preview = imagecreatefrompng($filepath);
|
$image = imagecreatefrompng($filepath);
|
||||||
break;
|
break;
|
||||||
case "image/webp":
|
case "image/webp":
|
||||||
$preview = imagecreatefromwebp($filepath);
|
$image = imagecreatefromwebp($filepath);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$preview = errorimage("Unsupported image type.");
|
$image = errorimage("Unsupported image type.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$raw_width = imagesx($preview);
|
$raw_width = imagesx($image);
|
||||||
$raw_height = imagesy($preview);
|
$raw_height = imagesy($image);
|
||||||
$aspect_ratio = $raw_width / $raw_height;
|
|
||||||
|
|
||||||
$target_size = 512;
|
$image = resize_image($image, $target_size);
|
||||||
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)
|
header("content-type: $output_mime");
|
||||||
|
switch($output_mime)
|
||||||
{
|
{
|
||||||
$preview_width = $target_size;
|
case "image/jpeg":
|
||||||
$preview_height = $target_size * $aspect_ratio;
|
imagejpeg($image);
|
||||||
|
break;
|
||||||
|
case "image/png":
|
||||||
|
imagepng($image);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case "image/webp":
|
||||||
|
imagewebp($image);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
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;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
http_response_code(501);
|
||||||
|
exit("Unrecognised file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo render a preview here
|
// todo render a preview here
|
||||||
|
@ -1482,6 +1497,24 @@ function errorimage($text)
|
||||||
return $image;
|
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",
|
"author": "Starbeamrainbowlabs",
|
||||||
"description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
|
"description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
|
||||||
"id": "feature-upload",
|
"id": "feature-upload",
|
||||||
"lastupdate": 1445630469,
|
"lastupdate": 1445677429,
|
||||||
"optional": false
|
"optional": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -138,68 +138,62 @@ register_module([
|
||||||
$filepath = $pageindex->{$env->page}->uploadedfilepath;
|
$filepath = $pageindex->{$env->page}->uploadedfilepath;
|
||||||
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// 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, "/")))
|
switch(substr($mime_type, 0, strpos($mime_type, "/")))
|
||||||
{
|
{
|
||||||
case "image":
|
case "image":
|
||||||
$preview = false;
|
$image = false;
|
||||||
switch($mime_type)
|
switch($mime_type)
|
||||||
{
|
{
|
||||||
case "image/jpeg":
|
case "image/jpeg":
|
||||||
$preview = imagecreatefromjpeg($filepath);
|
$image = imagecreatefromjpeg($filepath);
|
||||||
break;
|
break;
|
||||||
case "image/gif":
|
case "image/gif":
|
||||||
$preview = imagecreatefromgif($filepath);
|
$image = imagecreatefromgif($filepath);
|
||||||
break;
|
break;
|
||||||
case "image/png":
|
case "image/png":
|
||||||
$preview = imagecreatefrompng($filepath);
|
$image = imagecreatefrompng($filepath);
|
||||||
break;
|
break;
|
||||||
case "image/webp":
|
case "image/webp":
|
||||||
$preview = imagecreatefromwebp($filepath);
|
$image = imagecreatefromwebp($filepath);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$preview = errorimage("Unsupported image type.");
|
$image = errorimage("Unsupported image type.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$raw_width = imagesx($preview);
|
$raw_width = imagesx($image);
|
||||||
$raw_height = imagesy($preview);
|
$raw_height = imagesy($image);
|
||||||
$aspect_ratio = $raw_width / $raw_height;
|
|
||||||
|
|
||||||
// Determine the target size of the image
|
$image = resize_image($image, $target_size);
|
||||||
$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
|
header("content-type: $output_mime");
|
||||||
$preview_width = $raw_width;
|
switch($output_mime)
|
||||||
$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)
|
case "image/jpeg":
|
||||||
{
|
imagejpeg($image);
|
||||||
$preview_width = $target_size;
|
break;
|
||||||
$preview_height = $target_size * $aspect_ratio;
|
case "image/png":
|
||||||
}
|
imagepng($image);
|
||||||
else
|
break;
|
||||||
{
|
default:
|
||||||
$preview_height = $target_size;
|
case "image/webp":
|
||||||
$preview_width = $target_size * $aspect_ratio;
|
imagewebp($image);
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -272,4 +266,22 @@ function errorimage($text)
|
||||||
return $image;
|
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"
|
"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
|
// 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
|
// 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,
|
// default location of the mime.types file on Linux. If you aren't using linux,
|
||||||
|
|
Loading…
Reference in a new issue