Speichern von Bildern eingefügt

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

View File

@@ -352,6 +352,52 @@ class TopicData
return true;
}
/**
* Erstellt eine Datei aus übergebenen Daten
* @param string $name Dateiname
* @param string $image Bilddaten als Dateiinhalt, z.B. von file_get_contents()
* @return bool true wenn erfolgreich, sonst false
*/
public function uploadImage(string $name, string $image): bool
{
$imageDirectory = Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/";
if (!is_dir($imageDirectory)) {
if (!mkdir($imageDirectory)) {
return false;
}
}
if(!file_put_contents("$imageDirectory/$name", $image)) {
return false;
}
return true;
}
/**
* Löscht alle derzeit gespeicherten Bilder des Themas
* @return bool true wenn erfolgreich, sonst false
*/
public function deleteAllImages(): bool
{
return Util::delete(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/");
}
/**
* Löscht ein Bild des Themas
* @param string $name Dateiname
* @return bool true, wenn erfolgreich, sonst false
*/
public function deleteImage(string $name): bool
{
if (!unlink(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/$name")) {
return false;
}
return true;
}
/**
* Prüft für alle verwandten Themen, ob diese auch existieren. Wenn nicht, wird es aus der Liste entfernt
* @return bool true, wenn Elemente entfernt wurden, sonst false
@@ -402,20 +448,6 @@ class TopicData
return $changed;
}
/**
* Löscht ein Bild des Themas
* @param string $name Dateiname
* @return bool true, wenn erfolgreich, sonst false
*/
public function deleteImage(string $name): bool
{
if (!unlink(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/$name")) {
return false;
}
return true;
}
/**
* Löscht das Thema inklusive aller zugehörigen Dateien
* @return bool true, wenn erfolgreich gelöscht, sonst false
@@ -517,7 +549,7 @@ class TopicData
*/
public function getFinishedArticle(): string
{
$a = str_replace('$TOPICPATH', Config::getTopicDirectory($this->subjectId, $this->id) . "images", $this->article);
$a = str_replace('__TOPICPATH__', Config::getTopicDirectory($this->subjectId, $this->id) . "images", $this->article);
return $a;
}

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();