Files
cinema-display/server/cinema/lib/common/image_resizer.dart
Peter Siegmund efd34616b1
All checks were successful
Build and Push Multi-Arch Docker Image / build-and-push (push) Successful in 15m32s
some code cleanup and dependency updates
Signed-off-by: Peter Siegmund <developer@mars3142.org>
2025-12-08 16:09:44 +01:00

45 lines
1.0 KiB
Dart

import 'package:image/image.dart' as img;
import 'package:injectable/injectable.dart';
@singleton
class ImageResizer {
static const int maxWidth = 480;
static const int maxHeight = 320;
img.Image resizeToFit(img.Image image) {
final w = image.width;
final h = image.height;
if (w <= maxWidth && h <= maxHeight) {
return image;
}
final aspectRatio = w / h;
int newWidth;
int newHeight;
if (w > h) {
newWidth = maxWidth;
newHeight = (maxWidth / aspectRatio).round();
if (newHeight > maxHeight) {
newHeight = maxHeight;
newWidth = (maxHeight * aspectRatio).round();
}
} else {
newHeight = maxHeight;
newWidth = (maxHeight * aspectRatio).round();
if (newWidth > maxWidth) {
newWidth = maxWidth;
newHeight = (maxWidth / aspectRatio).round();
}
}
return img.copyResize(
image,
width: newWidth,
height: newHeight,
interpolation: img.Interpolation.linear,
);
}
}