<?php
/*
* Fotoalbum - Foto hochladen - foto.php (utf-8)
* - https://werner-zenk.de
*/
session_start();
if (!isset($_SESSION["user"])) {
header("Location: anmeldung.php");
exit;
}
include "einstellungen.php";
$output = "";
$error = [];
// Formular abgesendet
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Verzeichnisse vorhanden
if (!file_exists($directory))
$error[] = 'Das Verzeichnis: "' . $directory . '" ist nicht vorhanden!';
if (!file_exists($directoryThump))
$error[] = 'Das Verzeichnis: "' . $directoryThump . '" ist nicht vorhanden!';
// Schreibrechte
if (!is_writeable($directory))
$error[] = 'Das Verzeichnis: "' . $directory . '" besitzt keine Schreibrechte!';
if (!is_writeable($directoryThump))
$error[] = 'Das Verzeichnis: "' . $directoryThump . '" besitzt keine Schreibrechte!';
// Datei ausgewählt
if (!isset($_FILES["photo"]["name"]) ||
empty($_FILES["photo"]["name"]))
$error[] = 'Bitte wählen Sie ein Foto aus!';
// Fehler beim hochladen
if ($_FILES["photo"]["error"])
$error[] = 'Fehler beim hochladen: ' . $_FILES["photo"]["error"];
// Temporäre Datei wurde hochgeladen
if (is_uploaded_file($_FILES["photo"]["tmp_name"])) {
// Infos über die Datei holen
list($tmpWidth, $tmpHeight, $tmpType) = getImageSize($_FILES["photo"]["tmp_name"]);
$path = pathinfo($_FILES["photo"]["name"]);
$extension = strtolower($path["extension"]);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfoMimeType = finfo_file($finfo, $_FILES["photo"]["tmp_name"]);
finfo_close($finfo);
// Dateiformat überprüfen (ist es ein Bild [JPG-Format])
if ($tmpWidth < 1 ||
$tmpHeight < 1 ||
$tmpType != 2 ||
!in_array($finfoMimeType, $mimeType) ||
!in_array($_FILES["photo"]["type"], $mimeType) ||
!in_array($extension, array_keys($mimeType)))
$error[] = 'Falsches Dateiformat, erlaubt ist nur das JPG-Bildformat!';
// Max. Breite und Höhe
if ($tmpWidth > $maxWidth)
$error[] = 'Die Breite des Fotos ist leider zu groß, max: ' . $maxWidth . ' Pixel!';
if ($tmpHeight > $maxHeight)
$error[] = 'Die Höhe des Fotos ist leider zu groß, max: ' . $maxHeight . ' Pixel!';
// Seitenverhältnis überprüfen
if ($aspectRatio == "Querformat" &&
$tmpHeight > $tmpWidth)
$error[] = 'Erlaubt sind nur Fotos im Querformat!';
if ($aspectRatio == "Hochformat" &&
$tmpWidth > $tmpHeight)
$error[] = 'Erlaubt sind nur Fotos im Hochformat!';
// Max. Dateigröße
if (($_FILES["photo"]["size"] > $maxFilesize))
$error[] = 'Die Datei „' . $_FILES["photo"]["name"] . '” ist mit ' .
number_format(($_FILES["photo"]["size"] / 1024 / 1024), 1, ",", ".") . ' MB leider zu groß!';
// Kein Fehler!
if (count($error) == 0) {
// Dateiname filtern
$filename = utf8_decode($path["filename"]);
$filename = preg_replace("/[^a-z0-9_-]/", "", strtolower(strtr($filename, "äöüß ", "aous_")));
$newname = substr($filename, 0, 45) . "." . $extension;
// Datei vorhanden -> umbenennen
if (file_exists($directory . $newname))
$newname = substr($filename, 0, 45) . "_" . mt_rand(1, 999) . "." . $extension;
// Datei verschieben
if (move_uploaded_file($_FILES["photo"]["tmp_name"], $directory . $newname)) {
$output = '<p>✔ Das Foto wurde hochgeladen ';
// Thumbnail erstellen
thumbnail($newname, $directory, $directoryThump, $thumbWidth, $thumbHeight);
// Foto erstellen
photo($newname, $directory, $photoWidth, $photoHeight, $photoQuality, $waterSign, $waterSignPic, $waterSignTran);
# In die DB-Tabelle eintragen
if ($_POST["update"] == "") {
// Anzahl der Einträge
$select = $db->query("SELECT `id` FROM `fotoalbum`");
$nr = count($select->fetchAll(PDO::FETCH_OBJ));
$nr++;
// Daten eintragen
$date = date("Y-m-d H:i:00");
$insert = $db->prepare("INSERT INTO `fotoalbum`
(`nr`, `user`, `text`, `photo`, `view`, `date`)
VALUES (:nr, :user, :text, :photo, :view, :date)");
if ($insert->execute([":nr"=>$nr,
":user"=>$_SESSION["user"],
":text"=>strip_tags($_POST["text"]),
":photo"=>$newname,
":view"=>$photoView,
":date"=>$date])) {
$output .= 'und eingetragen.</p>';
}
}
# In der DB-Tabelle ersetzen
else {
// Berechtigung des Benutzers überprüfen
$select = $db->prepare("SELECT `user`, `photo` FROM `fotoalbum` WHERE `id` = :id");
$select->execute([":id"=>$_POST["update"]]);
$foto = $select->fetch();
if ($foto["user"] == $_SESSION["user"] ||
$_SESSION["user"] == $USER) {
// Daten ersetzen
$update = $db->prepare("UPDATE `fotoalbum`
SET `text`= :text,
`photo` = :photo
WHERE `id`= :id");
if ($update->execute([":text"=>strip_tags($_POST["text"]),
":photo"=>$newname,
":id"=>$_POST["update"]])) {
$output .= 'und ersetzt.</p>';
// Alte Fotos löschen
if ($foto["photo"] != $newname) {
unlink($directory . $foto["photo"]);
unlink($directoryThump . '_' .$foto["photo"]);
}
}
}
else {
$output = '<p>✘ Sie haben keine Berechtigung!</p>';
}
}
}
else {
$output = '<p>✘ Beim hochladen ist ein Fehler aufgetreten!</p>';
}
}
else {
$output = '<p>✘ ' . implode('<br>✘ ', $error) . '</p>';
}
}
}
// Thumbnail
function thumbnail($filename, $directory, $directoryThump, $thumbWidth, $thumbHeight) {
$info = getImageSize($directory . $filename);
// Neues Foto erzeugen
$create = imageCreatefromJPEG($directory . $filename);
// Abmessungen reduzieren
if ($thumbWidth && ($info[0] < $info[1])) {
$thumbWidth = ($thumbHeight / $info[1]) * $info[0];
}
else {
$thumbHeight = ($thumbWidth / $info[0]) * $info[1];
}
// Foto mit neuen Abmessungen kopieren
$thumbnail = imageCreateTruecolor($thumbWidth, $thumbHeight);
imageCopyResampled($thumbnail, $create, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $info[0], $info[1]);
// Foto überschreiben
imageJPEG($thumbnail, $directoryThump . "_" . $filename, 50);
imageDestroy($thumbnail);
}
// Foto
function photo($filename, $directory, $photoWidth, $photoHeight, $photoQuality, $waterSign, $waterSignPic, $waterSignTran) {
$info = getImageSize($directory . $filename);
// Neues Foto erzeugen
$create = imageCreatefromJPEG($directory . $filename);
// Abmessungen reduzieren
if ($photoWidth && ($info[0] < $info[1])) {
$photoWidth = ($photoHeight / $info[1]) * $info[0];
}
else {
$photoHeight = ($photoWidth / $info[0]) * $info[1];
}
// Foto mit neuen Abmessungen kopieren
$photo = imageCreateTruecolor($photoWidth, $photoHeight);
imageCopyResampled($photo, $create, 0, 0, 0, 0, $photoWidth, $photoHeight, $info[0], $info[1]);
// Wasserzeichen hinzufügen
if ($waterSign == "ja") {
$ws = imageCreateFromPng($waterSignPic);
$wsW = imageSX($ws);
$wsH = imageSY($ws);
$x = $photoWidth - $wsW - 10;
$y = $photoHeight - $wsH - 10;
imageCopyMerge($photo, $ws, $x, $y, 0, 0, $wsW, $wsH, $waterSignTran);
}
// Foto überschreiben
imageJPEG($photo, $directory . $filename, $photoQuality);
imageDestroy($photo);
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Fotoalbum - Foto hochladen</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script>
// Eventhandler setzen
window.addEventListener("load", function () {
document.getElementById("datei").addEventListener("change", changeFile);
});
function changeFile(event) {
document.getElementById("output").innerHTML = "";
// Foto auslesen
if (event.target.files[0]) {
var leser = new FileReader();
leser.addEventListener("load", photoLoaded);
leser.readAsDataURL(event.target.files[0]);
}
}
// Foto anzeigen
function photoLoaded(event) {
var img = document.createElement("img");
img.setAttribute("class", "thumbnail");
img.setAttribute("src", event.target.result);
document.getElementById("output").appendChild(img);
document.getElementById("text").focus();
}
</script>
<style>
img.thumbnail {
width: 90%;
}
div#output {
max-width: 270px;
}
</style>
</head>
<body>
<h2>Fotoalbum</h2>
<nav>
<a href="editor.php">Editor</a>
- Foto
- <a href="suche.php">Suche</a>
- <a href="anmeldung.php?abmeldung">Abmelden</a>
</nav>
<form method="post" enctype="multipart/form-data">
<fieldset>
<legend>Foto <?=isset($_GET["update"]) ? 'ersetzen' : 'hochladen';?></legend>
<p>
<label>Foto auswählen: <br>
<input type="file" name="photo" id="datei" size="15" accept="image/*" required="required"></label><br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?=$maxFilesize;?>">
<input type="hidden" name="update" value="<?=isset($_GET["update"]) ? $_GET["update"] : '';?>">
<dfn>Dateiformat: JPG - Max. Dateigröße: <?=number_format(($maxFilesize / 1024 / 1024), 1, ",", ".");?> MB<br>
Max. Abmessungen: <?=$maxWidth;?> x <?=$maxHeight;?> Pixel</dfn>
</p>
<div id="output"></div>
<p>
<label>Beschreibung:<br>
<input type="text" size="35" name="text" id="text" required="required" spellcheck="true"></label>
</p>
<p>
<input type="submit" name="submit" value="Foto hochladen">
</p>
</fieldset>
</form>
<?=$output?>
</body>
</html>