getMobileWidthAndHeight static method
Get Width and Height for Mobile
@param originalWidth original width of media @param originalHeight original height of media
Implementation
static Tuple2<int, int> getMobileWidthAndHeight(
int? originalWidth, int? originalHeight) {
if (originalWidth == null || originalHeight == null) {
return const Tuple2(
Constants.mobileImageMaxWidth, Constants.mobileImageMaxHeight);
}
var newWidth = originalWidth;
var newHeight = originalHeight;
// First check if we need to scale width
if (originalWidth > Constants.mobileImageMaxWidth) {
//scale width to fit
newWidth = Constants.mobileImageMaxWidth;
//scale height to maintain aspect ratio
newHeight = (newWidth * originalHeight / originalWidth).round();
}
// then check if we need to scale even with the new height
if (newHeight > Constants.mobileImageMaxHeight) {
//scale height to fit instead
newHeight = Constants.mobileImageMaxHeight;
//scale width to maintain aspect ratio
newWidth = (newHeight * originalWidth / originalHeight).round();
}
return Tuple2(
newWidth > Constants.mobileImageMinWidth
? newWidth
: Constants.mobileImageMinWidth,
newHeight > Constants.mobileImageMinHeight
? newHeight
: Constants.mobileImageMinHeight,
);
}