HifiPlanet-Website/hifi/api/src/Controllers/UploadController.php
Maaxxs 31d8143500 Compress uploaded images automatically to speed up page loads
Photos were being served exactly as uploaded, at whatever resolution and
quality the source file had - a multi-megabyte camera/phone photo shown
in a small tile still cost the visitor its full download weight.

Uploads::compressImage() (new) downscales an image to a 1920px long edge
if it's larger, and re-encodes it (JPEG quality 82, WEBP quality 82, PNG
best compression) in place - same path, same format, just a lighter
file. UploadController::store() now runs every new upload through it
automatically, so this fixes itself going forward with no admin action
needed.

For images already sitting in uploads/ from before this change,
SettingsController::compressUploads() (new endpoint, Einstellungen ->
Datenbank -> "Bilder optimieren") re-processes every existing file in
place and reports how much space was reclaimed - covers exactly the
photos causing today's slow loads.

Uses GD (near-universally available on shared hosting, unlike Imagick)
and does nothing instead of erroring if the extension or an unrecognized
file type is encountered, so an environment without GD just keeps
today's behavior rather than breaking uploads.

Verified locally (GD wasn't enabled in this XAMPP install, enabled it to
test - same php.ini already had zip enabled for the git-updater feature):
a synthetic 3000x2000 JPEG dropped from ~1.86MB to ~279KB and a test PNG's
alpha transparency survived resizing intact. Did not run the bulk
compressor against this repo's own uploads/ folder, since ~200 of those
files are git-tracked seed/demo images, not disposable local test data.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-11 03:23:31 +02:00

54 lines
1.7 KiB
PHP

<?php
namespace App\Controllers;
use App\Support\Http;
use App\Support\Uploads;
class UploadController
{
private const ALLOWED_MIME = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/webp' => 'webp',
];
private const MAX_BYTES = 5 * 1024 * 1024;
public static function store(): void
{
if (empty($_FILES['file'])) {
Http::error('Keine Datei übermittelt', 422);
}
$file = $_FILES['file'];
if ($file['error'] !== UPLOAD_ERR_OK) {
Http::error('Upload fehlgeschlagen', 422);
}
if ($file['size'] > self::MAX_BYTES) {
Http::error('Datei zu groß (max. 5 MB)', 422);
}
$mime = mime_content_type($file['tmp_name']);
if (!isset(self::ALLOWED_MIME[$mime])) {
Http::error('Nur JPG, PNG oder WEBP erlaubt', 422);
}
$extension = self::ALLOWED_MIME[$mime];
$filename = bin2hex(random_bytes(16)) . '.' . $extension;
$destination = __DIR__ . '/../../uploads/' . $filename;
if (!move_uploaded_file($file['tmp_name'], $destination)) {
Http::error('Datei konnte nicht gespeichert werden', 500);
}
// Verkleinert/komprimiert direkt beim Hochladen - vermeidet, dass ein zu grosses/zu
// hochaufgeloestes Foto die Seite spuerbar verlangsamt, ohne dass sich am
// gespeicherten Pfad/Format etwas aendert.
Uploads::compressImage($destination);
$config = require __DIR__ . '/../../config/config.php';
$prefix = rtrim($config['app']['base_path'], '/') . '/api/uploads/';
Http::send(['path' => $prefix . $filename], 201);
}
}