Speichern von Bildern eingefügt

This commit is contained in:
Matthias Grief
2025-01-02 14:32:35 +01:00
parent ee5d41d478
commit c065747d64
2 changed files with 89 additions and 17 deletions

View File

@@ -80,9 +80,41 @@ if($_SERVER['REQUEST_METHOD'] == 'POST') {
$relatedTopics[] = $relatedTopic;
}
$article = htmlentities($_POST['article'], ENT_HTML401, 'UTF-8');
$dom = new DOMDocument();
$dom->loadHTML($_POST['article']);
$article = $dom->textContent;
$dom->encoding = 'UTF-8';
$dom->loadHTML($article, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$htmlImages = $dom->getElementsByTagName('img');
$newImages = array();
foreach ($htmlImages as $htmlImage) {
$src = $htmlImage->getAttribute('src');
if(str_starts_with($src, "data:image")) {
$image = file_get_contents($src);
if (!$image) {
continue;
}
$image_format = false;
if (str_starts_with($src, 'data:image/jpeg;')) {
$image_format = "jpg";
} else if (str_starts_with($src, 'data:image/png;')) {
$image_format = "png";
}
if (!$image_format) {
continue;
}
$imagename = uniqid("image_", true) . ".$image_format";
$newImages[$imagename] = $image;
} else {
$imagePath = explode("/", $src);
$imagename = end($imagePath);
}
$htmlImage->setAttribute("src", '__TOPICPATH__/' . $imagename);
}
$article = mb_convert_encoding($dom->saveHTML(), 'UTF-8', 'HTML-ENTITIES');
if(isset($allSubjects[$_POST['subjectId']]->getTopics()[$_POST['id']])) {
$newTopic = $allSubjects[$_POST['subjectId']]->getTopics()[$_POST['id']];
@@ -101,6 +133,10 @@ if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!$newTopic) {
$errors["error"] = "Fehler beim Speichern des Themas.";
} else {
foreach ($newImages as $name => $image) {
$newTopic->uploadImage($name, $image);
}
if($_POST['submit'] == "Thema löschen") {
$newTopic->delete();
header("Location: " . "subject.php?subject=" . $_POST['subjectId']);
@@ -276,6 +312,10 @@ if($_SERVER['REQUEST_METHOD'] == 'POST') {
let html = quill.getSemanticHTML().replace(/ /g, " ");
html = html.replaceAll("<p></p>", "<br>");
while (html.endsWith("<br>")) {
html = html.replace(/<br>$/g, "");
}
document.getElementById('contentPreview').innerHTML = html;
document.getElementById('article-upload-field').value = html;
renderFormulas();