JPEG instead of PNG
This commit is contained in:
@@ -50,26 +50,25 @@ class Media extends DB\SQL\Mapper
|
|||||||
// pour dériver un texte alternatif lisible.
|
// pour dériver un texte alternatif lisible.
|
||||||
public function upload(string $srcPath, string $originalName = ''): int
|
public function upload(string $srcPath, string $originalName = ''): int
|
||||||
{
|
{
|
||||||
// Image::dump() gère le chargement, la transparence et la compression.
|
// Image::dump() gère le chargement et la compression.
|
||||||
// $path='' indique à F3 que le chemin est absolu.
|
// Attention : en JPEG, la transparence n'est pas conservée.
|
||||||
try {
|
try {
|
||||||
$img = new Image($srcPath, false, '');
|
$img = new Image($srcPath, false, '');
|
||||||
} catch (Throwable) {
|
} 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().
|
$fileName = bin2hex(random_bytes(16)) . '.jpg';
|
||||||
@unlink($srcPath);
|
|
||||||
|
|
||||||
$fileName = bin2hex(random_bytes(16)) . '.png';
|
|
||||||
$target = app_public_media_dir() . '/' . $fileName;
|
$target = app_public_media_dir() . '/' . $fileName;
|
||||||
|
|
||||||
if (!Base::instance()->write($target, $data)) {
|
if (!Base::instance()->write($target, $data)) {
|
||||||
throw new RuntimeException('Impossible d\'enregistrer cette image.');
|
throw new RuntimeException('Impossible d\'enregistrer cette image.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@unlink($srcPath);
|
||||||
|
|
||||||
$this->reset();
|
$this->reset();
|
||||||
$this->file_name = $fileName;
|
$this->file_name = $fileName;
|
||||||
$this->alt = $originalName !== '' ? self::altFromFilename($originalName) : '';
|
$this->alt = $originalName !== '' ? self::altFromFilename($originalName) : '';
|
||||||
|
|||||||
Reference in New Issue
Block a user