All checks were successful
Build and Push Multi-Arch Docker Image / build-and-push (push) Successful in 15m32s
Signed-off-by: Peter Siegmund <developer@mars3142.org>
45 lines
1.0 KiB
Dart
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,
|
|
);
|
|
}
|
|
}
|