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, ); } }