Fix: Bild-Uploads wieder funktionsfähig machen
- Upload-Limits auf 25 MB pro Bild bzw. 64 MB pro Request erhöht (public/.user.ini für FPM/CGI, docker/php/uploads.ini für Docker) - Docker: storage/-Verzeichnis beim Start auf www-data umstellen - Image::store() prüft Upload-Fehlercode, Bildformat, GD/JPEG und Schreibrechte und wirft verständliche Fehler statt still zu scheitern - Galerie, Benutzer- und Tour-Bild zeigen Fehler als Flash-Meldung - post_max_size-Überschreitung wird vor der CSRF-Prüfung abgefangen - Slug wandelt Großbuchstaben-Umlaute korrekt um - Diagnose-Script bin/check-upload.php ergänzt
This commit is contained in:
parent
8bde8d143e
commit
d8c9ec8ef8
16 changed files with 486 additions and 35 deletions
|
|
@ -14,33 +14,82 @@ namespace App\Core;
|
|||
* Hinweis: kein imagedestroy() aufrufen. Seit PHP 8.0 werden GdImage-Objekte
|
||||
* automatisch freigegeben; imagedestroy() ist seit PHP 8.5 deprecated und
|
||||
* erzeugt Warnungen, die Redirects (header()) kaputt machen können.
|
||||
*
|
||||
* Fehler werden bewusst als RuntimeException geworfen, damit die aufrufenden
|
||||
* Controller sie dem Benutzer anzeigen können, statt still zu scheitern.
|
||||
*/
|
||||
class Image
|
||||
{
|
||||
/** Speichert ein hochgeladenes Bild unter storage/img_storage/<category>/. */
|
||||
public static function store(array $upload, string $category, string $name): void
|
||||
/**
|
||||
* Speichert ein hochgeladenes Bild unter storage/img_storage/<category>/.
|
||||
*
|
||||
* @throws \RuntimeException wenn der Upload unvollständig, das Format nicht
|
||||
* lesbar oder das Zielverzeichnis nicht beschreibbar ist
|
||||
*/
|
||||
public static function store(array $upload, string $category, string $name): bool
|
||||
{
|
||||
if (empty($upload['tmp_name']) || !is_uploaded_file($upload['tmp_name'])) {
|
||||
return;
|
||||
// Ohne JPEG-Unterstützung in GD schlägt imagejpeg() sonst mit einem
|
||||
// Fatal Error fehl, der (ohne display_errors) unsichtbar bleibt.
|
||||
if (!function_exists('imagecreatefromstring') || !function_exists('imagejpeg') || (imagetypes() & IMG_JPG) !== IMG_JPG) {
|
||||
error_log('Image::store: GD-Erweiterung fehlt oder wurde ohne JPEG-Unterstützung gebaut.');
|
||||
|
||||
throw new \RuntimeException('Die Bildverarbeitung auf dem Server unterstützt kein JPEG. Bitte den Administrator informieren (GD mit libjpeg installieren).');
|
||||
}
|
||||
|
||||
$error = (int) ($upload['error'] ?? UPLOAD_ERR_OK);
|
||||
|
||||
if ($error !== UPLOAD_ERR_OK) {
|
||||
throw new \RuntimeException(upload_error_message($error) ?? 'Der Upload ist fehlgeschlagen.');
|
||||
}
|
||||
|
||||
$tmp = (string) ($upload['tmp_name'] ?? '');
|
||||
|
||||
if ($tmp === '' || !is_uploaded_file($tmp)) {
|
||||
throw new \RuntimeException('Die hochgeladene Datei konnte nicht gelesen werden.');
|
||||
}
|
||||
|
||||
// Format anhand des Inhalts prüfen (nicht anhand der Dateiendung).
|
||||
$info = @getimagesize($tmp);
|
||||
|
||||
if ($info === false) {
|
||||
throw new \RuntimeException('Die Datei ist kein lesbares Bild. Unterstützt werden JPG, PNG, GIF, WebP und BMP. HEIC/HEIF von iPhones bitte vorher in JPG umwandeln.');
|
||||
}
|
||||
|
||||
$type = (int) ($info[2] ?? 0);
|
||||
|
||||
if (!in_array($type, [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_WEBP, IMAGETYPE_BMP], true)) {
|
||||
throw new \RuntimeException('Dieses Bildformat wird nicht unterstützt. Bitte JPG, PNG, GIF, WebP oder BMP verwenden.');
|
||||
}
|
||||
|
||||
[$w, $h] = [(int) $info[0], (int) $info[1]];
|
||||
|
||||
if ($w < 1 || $h < 1) {
|
||||
throw new \RuntimeException('Das Bild hat keine gültige Größe.');
|
||||
}
|
||||
|
||||
$dir = config('paths.img_storage') . '/' . self::slug($category);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
// 0775 genügt, wenn PHP-FPM als Eigentümer/Gruppe läuft; 0777 hilft
|
||||
// bei Bind-Mounts mit abweichender UID.
|
||||
@mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
if (!is_dir($dir) || !is_writable($dir)) {
|
||||
throw new \RuntimeException('Der Bildspeicher auf dem Server ist nicht beschreibbar. Bitte den Administrator informieren.');
|
||||
}
|
||||
|
||||
$base = $dir . '/' . self::slug(pathinfo($name, PATHINFO_FILENAME));
|
||||
|
||||
$data = @file_get_contents($upload['tmp_name']);
|
||||
$src = $data === false ? null : @imagecreatefromstring($data);
|
||||
$data = @file_get_contents($tmp);
|
||||
$src = $data === false ? false : @imagecreatefromstring($data);
|
||||
|
||||
if ($src === false || $src === null) {
|
||||
return;
|
||||
if (!$src instanceof \GdImage) {
|
||||
error_log('Image::store: imagecreatefromstring() fehlgeschlagen für ' . $tmp);
|
||||
|
||||
throw new \RuntimeException('Das Bild konnte nicht verarbeitet werden. Bitte ein anderes Format verwenden.');
|
||||
}
|
||||
|
||||
[$w, $h] = [imagesx($src), imagesy($src)];
|
||||
|
||||
// Große Ansicht
|
||||
if ($w > $h) {
|
||||
self::resize($src, $base . '_gross.jpg', 1200, (int) round($h * 1200 / $w));
|
||||
|
|
@ -54,20 +103,51 @@ class Image
|
|||
} else {
|
||||
self::resize($src, $base . '.jpg', (int) round($w * 256 / $h), 256);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function resize(\GdImage $src, string $target, int $newW, int $newH): void
|
||||
{
|
||||
$newW = max(1, $newW);
|
||||
$newH = max(1, $newH);
|
||||
|
||||
$dst = imagecreatetruecolor($newW, $newH);
|
||||
|
||||
if ($dst === false) {
|
||||
error_log('Image::resize: imagecreatetruecolor() fehlgeschlagen für ' . $target);
|
||||
|
||||
throw new \RuntimeException('Das Bild konnte nicht verarbeitet werden.');
|
||||
}
|
||||
|
||||
// Transparente PNG/GIF/WebP bekommen einen weißen Hintergrund,
|
||||
// da das Ergebnis als JPEG gespeichert wird.
|
||||
$white = imagecolorallocate($dst, 255, 255, 255);
|
||||
|
||||
if ($white !== false) {
|
||||
imagefilledrectangle($dst, 0, 0, $newW, $newH, $white);
|
||||
}
|
||||
|
||||
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newW, $newH, imagesx($src), imagesy($src));
|
||||
imagejpeg($dst, $target, 85);
|
||||
|
||||
if (!@imagejpeg($dst, $target, 85)) {
|
||||
error_log('Image::resize: imagejpeg() fehlgeschlagen für ' . $target);
|
||||
|
||||
throw new \RuntimeException('Das Bild konnte nicht gespeichert werden (Schreibrechte prüfen).');
|
||||
}
|
||||
}
|
||||
|
||||
/** Erzeugt einen sicheren, URL-tauglichen Slug. */
|
||||
public static function slug(string $value): string
|
||||
{
|
||||
$value = strtolower(trim($value));
|
||||
$value = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $value);
|
||||
// Umlaute vor dem Kleinschreiben ersetzen: strtolower() wandelt nur
|
||||
// ASCII, "Ä" würde sonst zu "rger" statt "aer..." werden.
|
||||
$value = str_replace(
|
||||
['Ä', 'Ö', 'Ü', 'ẞ', 'ä', 'ö', 'ü', 'ß'],
|
||||
['ae', 'oe', 'ue', 'ss', 'ae', 'oe', 'ue', 'ss'],
|
||||
trim($value)
|
||||
);
|
||||
$value = strtolower($value);
|
||||
$value = preg_replace('/[^a-z0-9]+/', '-', $value) ?? '';
|
||||
$value = trim($value, '-');
|
||||
|
||||
|
|
|
|||
|
|
@ -65,6 +65,13 @@ class Router
|
|||
|
||||
// CSRF-Schutz für verändernde Methoden
|
||||
if (in_array($method, ['POST', 'PUT', 'DELETE', 'PATCH'], true)) {
|
||||
// Bei überschrittenem post_max_size sind $_POST und $_FILES leer.
|
||||
// Ohne diese Prüfung würde der Request als CSRF-Fehler enden.
|
||||
if (post_too_large()) {
|
||||
flash_set('error', 'Der Upload ist zu groß (Server-Limit: ' . ini_get('post_max_size') . '). Bitte kleinere Dateien verwenden.');
|
||||
back();
|
||||
}
|
||||
|
||||
verify_csrf();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue