base64_decodierer.php
Quell Code
<?php
define ('MAX_KB', 64);
define ('DEBUG', true);
/* Main */
if (DEBUG) {
error_reporting(E_ALL);
ini_set("display_errors", 1);
} else {
error_reporting(0);
ini_set("display_errors", 0);
}
define('START_TIME', microtime(true));
define ('MAX_UPLOAD_FILE_SIZE', MAX_KB * 1024);
// Leeres Formular:
if ( empty($_POST['typ']) ) {
$_POST['typ']='html';
sendForm();
rm_and_exit();}
// falsche Angaben?
if ( $_POST['typ'] != 'html' && $_POST['typ'] != 'css' && $_POST['typ'] != 'base64') {
sendForm(); rm_and_exit();} // Übertragungsfehler?
if ( $_FILES['Datei']['error'] ) {
sendForm('Sie haben keine Datei ausgewählt oder bei der Übertragung trat ein Fehler auf.');
rm_and_exit();} // Über der eingeschränkten Dateigröße?
if ($_FILES['Datei']['size'] > MAX_UPLOAD_FILE_SIZE) { sendForm('Die gesendete Datei ist zu groß!'); rm_and_exit();}
// Mimetype holen:
$mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $_FILES['Datei']['tmp_name']);
if ( 'image/svg+xml' != $mimeType && 'image/jpeg' != $mimeType && 'image/jpg' != $mimeType && 'image/gif' != $mimeType && 'image/png' != $mimeType && 'image/bmp' != $mimeType ) {
sendForm('Die gesendete Datei "' . htmlspecialchars($_FILES['Datei']['name']) . '" hat den Mime-Typ "' . htmlspecialchars($mimeType) . '" und somit kein in HTML zulässiges Grafikformat! (PNG, JPEG, BMP, GIF)'); rm_and_exit();}
//Aktionen:
if ($_POST['typ'] == 'base64') { header ('Content-Type: text/plain, charset=US-ASCII');
echo wordwrap(base64_encode(file_get_contents($_FILES['Datei']['tmp_name'])), 77, "\n", true);
rm_and_exit();} if ( ! list ($width, $height, $type, $attr) = getimagesize($_FILES['Datei']['tmp_name'])) {
if ('image/svg+xml' == $mimeType) { $width=256; $height=256; } else {
sendForm('Die Größe der gesendete Datei "' . htmlspecialchars($_FILES['Datei']['name']) . '" mit dem Mime-Typ "' . htmlspecialchars($mimeType) . '" konnte nicht ermittelt werden. Die Datei ist sehr wahrscheinlich fehlerhaft!');
rm_and_exit(); }} if ($_POST['typ'] == 'html') {
$base64 = wordwrap(base64_encode(file_get_contents($_FILES['Datei']['tmp_name'])), 77, "\n", true);
$Kilobytes = '~' . round(strlen($base64)/1024) . ' KBytes'; $s = "<img width='$width' height='$height' alt='".htmlspecialchars($_FILES['Datei']['name'])."' src='data:$mimeType;base64,\n$base64'>"; $s = '<p><strong>Test:</strong></p>' . $s . "<p><strong>Ihre codierte Grafik ($Kilobytes) zum Kopieren und Einfügen: <noscript>(Mit Javascript genügt ein Klick zum Markieren!)</noscript></strong></p><div contenteditable='false' id='output'>" . str_replace("\n", '<br>', htmlspecialchars($s, ENT_QUOTES)) . '</div>';
sendForm(false, $s); rm_and_exit();} if ($_POST['typ'] == 'css') {
$base64=base64_encode(file_get_contents($_FILES['Datei']['tmp_name'])); $Kilobytes= '~' . round(strlen($base64)/1024) . ' KBytes'; $s = "background-image:url('data:$mimeType;base64,$base64');"; $widthpx = $width . 'px'; $heightpx = $height . 'px'; $s="<p><strong>Test:</strong></p><div style='width:$widthpx; height:$heightpx; background-image:url(\"data:$mimeType;base64,$base64\");'> </div><p><strong>Ihre codierte Grafik ($Kilobytes) zum Kopieren und Einfügen: <span id='noJS'>(Mit Javascript genügt ein Klick zum Markieren!)</strong></p><div contenteditable='false' id='output'>" . htmlspecialchars($s, ENT_QUOTES) . '</div>'; sendForm(false, $s); rm_and_exit();} error_log('Das Skript hat unerwartet keinen Endpunkt gefunden.');echo 'Fataler Fehler: Hier lief was ganz falsch: Exit';rm_and_exit(); /* Funktionen */ function sendForm($errorText=false, $s=false) { $base64Checked = '';
$htmlChecked = ''; $cssChecked = ''; if ('base64' == $_POST['typ']) {
$base64Checked = 'checked'; } elseif ('css' == $_POST['typ']) { $cssChecked = 'checked'; } else {
$htmlChecked = 'checked'; }?><!DOCTYPE HTML><html> <head>
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"> <meta charset='utf-8'>
<title>Data-URI-Generator für Inline-Grafiken</title> <script> window.onload = function() {
if ( document.getElementById('output') ) {
document.getElementById('output').addEventListener('click', function () {
if ( document.selection ) { var range = document.body.createTextRange(); range.moveToElementText(this);
range.select();
} else if ( window.getSelection ) { var range = document.createRange(); range.selectNode(this);
window.getSelection().addRange(range); } } , false); } } </script>
<link rel="stylesheet" type="text/css" href="/standard.css" /> <style type="text/css"> #content {padding:1em 1em 1em 1em;}
p.errorText {background-color:#fdd;padding:.5em;border:1px solid #a00;} label:hover{text-decoration:underline;}
legend{font-weight:bold;font-size:90%; padding: 0 1em 0 1em; } fieldset{border:2px solid #ddd;border-radius:.5em;width:20em;margin-bottom:.5em;}
#output {width:auto;height:auto;font-family:monospace;font-size:.95em;border:3px solid gray;padding:.5em;margin-right:1em;
white-space:nowrap; overflow:scroll;} </style> </head> <body> <h1>Data-URI-Generator für Inline-Grafiken</h1>
<div id="content"> <?php if ($errorText) echo "<p class='errorText'><strong>Fehler:</strong> $errorText</p>"; ?>
<form id="myForm" action="" method="post" enctype="multipart/form-data">
<fieldset><legend>Datei: (maximal <?php echo MAX_KB ?> KibiBytes)</legend>
<input id="Datei" name="Datei" type="file" size="50" accept="image/*" required><br /> </fieldset> <fieldset><legend>Typ:</legend>
<input type="radio" <?php echo $htmlChecked; ?> name="typ" value="html" id="getHTML"><label for="getHTML">HTML</label><br />
<input type="radio" <?php echo $cssChecked; ?> name="typ" value="css" id="getCSS"><label for="getCSS">CSS (als Hintergrund)</label><br />
<input type="radio" <?php echo $base64Checked; ?> name="typ" value="base64" id="getBase64">
<label for="getBase64">nur base64-codiert (Plaintext)</label><br /> </fieldset>
<button id="mySubmit" type="submit">Data-URL generieren</button> </form> <?php if ($s) echo $s; ?>
<?php if ( (! empty($_SERVER['SERVER_NAME']) ) && ( false===strpos('home', $_SERVER['SERVER_NAME']) ) ) { $t=getExecTime(START_TIME); ?>
<p>Das Skript (<a href="https://code.fastix.org/Projekte/Data-URL-Generator/">Download</a>) hat auf einem
<?php echo getCPUInfo(); ?> und PHP <?php echo phpversion(), ' etwa ', $t; ?> Millisekunde(n) benötigt.</p> <?php } ?>
</div> </body></html><?php} function rm_and_exit() { if (! empty($_FILES['Datei']['tmp_name']) ) {
if ( is_file($_FILES['Datei']['tmp_name']) ) { unlink ($_FILES['Datei']['tmp_name']); } else {
error_log('Scheinbar wurde das Tempfile während der Laufzeit gelöscht.'); } }
exit;} function getCPUInfo() {} function getExecTime($startTime) { return round((microtime(true)-$startTime)*1000, 1);} ?>
</body></html>