JPEG instead of PNG

This commit is contained in:
julien
2026-03-27 17:20:13 +01:00
parent ced7dbfbf7
commit 0026fb32c2

View File

@@ -50,26 +50,25 @@ class Media extends DB\SQL\Mapper
// pour dériver un texte alternatif lisible.
public function upload(string $srcPath, string $originalName = ''): int
{
// Image::dump() gère le chargement, la transparence et la compression.
// $path='' indique à F3 que le chemin est absolu.
// Image::dump() gère le chargement et la compression.
// Attention : en JPEG, la transparence n'est pas conservée.
try {
$img = new Image($srcPath, false, '');
} catch (Throwable) {
throw new RuntimeException('Fichier image invalide ou format non supporté (JPG, PNG, WebP).');
throw new RuntimeException('Fichier image invalide ou format source non supporté.');
}
$data = $img->dump('png', 9); // sans perte, compression maximale
$data = $img->dump('jpeg', 85);
// Supprimer le fichier intermédiaire déposé par Web::receive().
@unlink($srcPath);
$fileName = bin2hex(random_bytes(16)) . '.png';
$fileName = bin2hex(random_bytes(16)) . '.jpg';
$target = app_public_media_dir() . '/' . $fileName;
if (!Base::instance()->write($target, $data)) {
throw new RuntimeException('Impossible d\'enregistrer cette image.');
}
@unlink($srcPath);
$this->reset();
$this->file_name = $fileName;
$this->alt = $originalName !== '' ? self::altFromFilename($originalName) : '';