|null $file Eintrag aus $_FILES * @return string Dateiname * @throws RuntimeException mit verstaendlicher Meldung */ function profile_store_upload(?array $file, string $prefix, int $maxSide): string { if (!$file || ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) { throw new RuntimeException('Bitte ein Bild auswählen.'); } if ($file['error'] !== UPLOAD_ERR_OK) { throw new RuntimeException(in_array($file['error'], [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE], true) ? 'Das Bild ist zu groß.' : 'Upload fehlgeschlagen.'); } if ($file['size'] > PROFILE_MAX_UPLOAD) { throw new RuntimeException('Das Bild ist zu groß (höchstens 8 MB).'); } $info = @getimagesize($file['tmp_name']); $types = [IMAGETYPE_PNG => 'png', IMAGETYPE_JPEG => 'jpg', IMAGETYPE_GIF => 'gif', IMAGETYPE_WEBP => 'webp']; if ($info === false || !isset($types[$info[2]])) { throw new RuntimeException('Nur PNG, JPG, GIF oder WEBP sind erlaubt.'); } $ext = $types[$info[2]]; $name = $prefix . '_' . bin2hex(random_bytes(8)) . '.' . $ext; $target = PROFILE_UPLOAD_DIR . $name; [$width, $height] = $info; $canResize = $ext !== 'gif' && function_exists('imagecreatefromstring') && max($width, $height) > $maxSide; if ($canResize) { $source = @imagecreatefromstring((string) file_get_contents($file['tmp_name'])); if ($source !== false) { $scale = $maxSide / max($width, $height); $newW = max(1, (int) round($width * $scale)); $newH = max(1, (int) round($height * $scale)); $resized = imagecreatetruecolor($newW, $newH); imagealphablending($resized, false); imagesavealpha($resized, true); imagecopyresampled($resized, $source, 0, 0, 0, 0, $newW, $newH, $width, $height); $saved = match ($ext) { 'png' => imagepng($resized, $target, 6), 'webp' => function_exists('imagewebp') && imagewebp($resized, $target, 85), default => imagejpeg($resized, $target, 86), }; imagedestroy($source); imagedestroy($resized); if ($saved) { return $name; } } } if (!move_uploaded_file($file['tmp_name'], $target)) { throw new RuntimeException('Das Bild konnte nicht gespeichert werden (Schreibrechte auf dem Server prüfen).'); } return $name; } function profile_delete_file(?string $filename): void { if ($filename && strpos($filename, '/') === false && is_file(PROFILE_UPLOAD_DIR . $filename)) { @unlink(PROFILE_UPLOAD_DIR . $filename); } } /** * Charaktere aller (oder bestimmter) Mitglieder, gruppiert nach user_id. Hauptcharakter zuerst. * * @return array>> */ function profile_characters_by_user(?int $userId = null): array { $sql = 'SELECT * FROM member_characters' . ($userId !== null ? ' WHERE user_id = ?' : '') . ' ORDER BY is_main DESC, sort_order, id'; $stmt = db()->prepare($sql); $stmt->execute($userId !== null ? [$userId] : []); $byUser = []; foreach ($stmt->fetchAll() as $row) { $byUser[(int) $row['user_id']][] = $row; } return $byUser; }