Refactor errorimage into core & greatly improve it

This commit is contained in:
Starbeamrainbowlabs 2019-10-20 21:42:13 +01:00
parent ca6546677b
commit 6d19af2e1b
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
4 changed files with 66 additions and 37 deletions

View File

@ -432,6 +432,58 @@ function system_extension_mime_type($ext) {
return isset($types[$ext]) ? $types[$ext] : null;
}
/**
* Creates an images containing the specified text.
* Useful for sending errors back to the client.
* @package core
* @param string $text The text to include in the image.
* @param int $target_size The target width to aim for when creating
* the image. Not not specified, a value is
* determined automatically.
* @return resource The handle to the generated GD image.
*/
function errorimage($text, $target_size = null)
{
$width = 0;
$height = 0;
$border_size = 10; // in px, if $target_size isn't null has no effect
$line_spacing = 2; // in px
$font_size = 5; // 1 - 5
$font_width = imagefontwidth($font_size); // in px
$font_height = imagefontheight($font_size); // in px
$text_lines = array_map("trim", explode("\n", $text));
if(!empty($target_size)) {
$width = $target_size;
$height = $target_size * (2 / 3);
}
else {
$height = count($text_lines) * $font_height +
(count($text_lines) - 1) * $line_spacing +
$border_size * 2;
foreach($text_lines as $line)
$width = max($width, $font_width * strlen($line));
$width += $border_size * 2;
}
$image = imagecreatetruecolor($width, $height);
imagefill($image, 0, 0, imagecolorallocate($image, 250, 249, 251)); // Set the background to #faf8fb
$i = 0;
foreach($text_lines as $line) {
imagestring($image, $font_size,
($width / 2) - (($font_width * strlen($line)) / 2),
$border_size + $i * ($font_height + $line_spacing),
$line,
imagecolorallocate($image, 68, 39, 113) // #442772
);
$i++;
}
return $image;
}
/**
* Generates a stack trace.
* @package core

View File

@ -162,10 +162,10 @@
{
"id": "feature-upload",
"name": "Uploader",
"version": "0.6.1",
"version": "0.6.2",
"author": "Starbeamrainbowlabs",
"description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File\/' prefix.",
"lastupdate": 1568296731,
"lastupdate": 1571603165,
"optional": false,
"extra_data": []
},
@ -337,7 +337,7 @@
"version": "0.10",
"author": "Emanuil Rusev & Starbeamrainbowlabs",
"description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation.",
"lastupdate": 1571601235,
"lastupdate": 1571603632,
"optional": false,
"extra_data": {
"Parsedown.php": "https:\/\/raw.githubusercontent.com\/erusev\/parsedown\/fe7a50eceb4a3c867cc9fa9c0aa906b1067d1955\/Parsedown.php",

View File

@ -1,7 +1,7 @@
<?php
register_module([
"name" => "Uploader",
"version" => "0.6.1",
"version" => "0.6.2",
"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",
@ -656,37 +656,4 @@ function getsvgsize($svgFilename)
return $imageSize;
}
/**
* Creates an images containing the specified text.
* Useful for sending errors back to the client.
* @package feature-upload
* @param string $text The text to include in the image.
* @param int $target_size The target width to aim for when creating
* the image.
* @return resource The handle to the generated GD image.
*/
function errorimage($text, $target_size = null)
{
$width = 640;
$height = 480;
if(!empty($target_size))
{
$width = $target_size;
$height = $target_size * (2 / 3);
}
$image = imagecreatetruecolor($width, $height);
imagefill($image, 0, 0, imagecolorallocate($image, 238, 232, 242)); // Set the background to #eee8f2
$fontwidth = imagefontwidth(3);
imagestring($image, 3,
($width / 2) - (($fontwidth * strlen($text)) / 2),
($height / 2) - (imagefontheight(3) / 2),
$text,
imagecolorallocate($image, 17, 17, 17) // #111111
);
return $image;
}
?>

View File

@ -52,6 +52,16 @@ register_module([
)));
});
add_action("parsedown-render-ext", function() {
global $settings, $env, $paths;
if(!isset($_GET["source"])) {
http_response_code(400);
header("content-type: image/png");
imagepng(errorimage("Error: No source text \nspecified."));
}
});
/*
* ███████ ████████ █████ ████████ ██ ███████ ████████ ██ ██████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██