Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dd87538
scaling and image quality done
balvinderz Sep 25, 2021
c3c1344
image scaling and quality done
balvinderz Sep 25, 2021
cf0ee4d
implement max width max height and imageQuality for web
balvinderz Sep 27, 2021
130f78a
added maxWidth max height and imageQuality for web
balvinderz Sep 27, 2021
b8d3d65
dartfmt
balvinderz Sep 27, 2021
be720c2
revert pubspec.yaml of image_picker
balvinderz Sep 27, 2021
754cec5
run format
balvinderz Sep 27, 2021
942ac46
added image resizer class
balvinderz Sep 30, 2021
982d40a
Merge branch 'master' of https://github.com/flutter/plugins into comp…
balvinderz Sep 30, 2021
6145cf5
Merge branch 'compress_file_web' of https://github.com/balvinderz/plu…
balvinderz Sep 30, 2021
91f92e6
fix calculate size logic
balvinderz Sep 30, 2021
b10408f
fix if condition in resizeImage
balvinderz Sep 30, 2021
42d8191
image resizer done
balvinderz Sep 30, 2021
019a738
added utils test'
balvinderz Sep 30, 2021
2b1842d
update readme
balvinderz Sep 30, 2021
5f7a0dd
add image resizer tests
balvinderz Sep 30, 2021
f61b965
dartfmt
balvinderz Sep 30, 2021
5b477e6
add licenses
balvinderz Sep 30, 2021
60e0b9f
remove path dependancy
balvinderz Sep 30, 2021
7a63323
fix typo and remove extra space
balvinderz Sep 30, 2021
c75b036
fix doc comment
balvinderz Sep 30, 2021
f5e6d45
remove double spaces
balvinderz Sep 30, 2021
d38de67
review changes
balvinderz Oct 2, 2021
5c352f6
fix typo
balvinderz Oct 4, 2021
08b8240
use completeError
balvinderz Oct 4, 2021
7151d71
remove unused imports
balvinderz Oct 4, 2021
a3bbc58
dartfmt
balvinderz Oct 4, 2021
d81990b
fix typo
balvinderz Oct 4, 2021
c90eff3
remove 1 doc comment
balvinderz Oct 4, 2021
811b3fb
update tests and resizing algorithm
balvinderz Oct 5, 2021
3e4d7c1
Review changes
balvinderz Oct 5, 2021
554e996
move image resizer files in src
balvinderz Oct 6, 2021
d15b8b1
dartfmt
balvinderz Oct 6, 2021
b87f0cf
dartfmt again
balvinderz Oct 6, 2021
d99a702
Fix some review comments
ditman Oct 16, 2021
5e825ab
Slight update in the CHANGELOG
ditman Oct 16, 2021
c4d057e
Merge branch 'master' into compress_file_web
ditman Oct 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tests and resizing algorithm
  • Loading branch information
balvinderz committed Oct 5, 2021
commit 811b3fb6dffe77b884385fa3c4b5293a467d3ca3
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ void main() {
name: pngHtmlFile.name, mimeType: pngHtmlFile.type);
});

testWidgets("image is loaded correctly ", (WidgetTester tester) async {
final imageElement = await imageResizer.loadImage(pngFile.path);
expect(imageElement.width!, 10);
expect(imageElement.height!, 10);
});

testWidgets(
"canvas is loaded with image's width and height when max width and max height are null",
(widgetTester) async {
final imageElement = await imageResizer.loadImage(pngFile.path);
final canvas = imageResizer.resizeImageElement(imageElement, null, null);
expect(canvas.width, imageElement.width);
expect(canvas.height, imageElement.height);
});

testWidgets(
"canvas size is scaled when max width and max height are not null",
(widgetTester) async {
final imageElement = await imageResizer.loadImage(pngFile.path);
final canvas = imageResizer.resizeImageElement(imageElement, 8, 8);
expect(canvas.width, 8);
expect(canvas.height, 8);
});

testWidgets("resized image is returned after converting canvas to file",
(widgetTester) async {
final imageElement = await imageResizer.loadImage(pngFile.path);
final canvas = imageResizer.resizeImageElement(imageElement, null, null);
final resizedImage =
await imageResizer.writeCanvasToFile(pngFile, canvas, null);
expect(resizedImage.name, "scaled_${pngFile.name}");
});

testWidgets("image is scaled when maxWidth is set",
(WidgetTester tester) async {
final scaledImage =
Expand All @@ -37,6 +70,7 @@ void main() {
final scaledImageSize = await _getImageSize(scaledImage);
expect(scaledImageSize, Size(5, 5));
});

testWidgets("image is scaled when maxHeight is set",
(WidgetTester tester) async {
final scaledImage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';

///a function that checks if an image needs to be resized or not
bool imageResizeNeeded(double? maxWidth, double? maxHeight, int? imageQuality) {
return imageQuality != null
Expand All @@ -24,41 +26,8 @@ bool isImageQualityValid(int imageQuality) {
/// maxHeight is the maximum height of the scaled image
Size calculateSizeOfDownScaledImage(
Size imageSize, double? maxWidth, double? maxHeight) {
double originalWidth = imageSize.width;
double originalHeight = imageSize.height;

bool hasMaxWidth = maxWidth != null;
bool hasMaxHeight = maxHeight != null;
double width = hasMaxWidth ? min(maxWidth, originalWidth) : originalWidth;
double height =
hasMaxHeight ? min(maxHeight, originalHeight) : originalHeight;
bool shouldDownscaleWidth = hasMaxWidth && maxWidth < originalWidth;
bool shouldDownscaleHeight = hasMaxHeight && maxHeight < originalHeight;
bool shouldDownscale = shouldDownscaleWidth || shouldDownscaleHeight;
if (shouldDownscale) {
double downscaledWidth =
((height / originalHeight) * originalWidth).floorToDouble();
double downscaledHeight =
((width / originalWidth) * originalHeight).floorToDouble();
if (width < height) {
if (!hasMaxWidth) {
width = downscaledWidth;
} else {
height = downscaledHeight;
}
} else if (height < width) {
if (!hasMaxHeight) {
height = downscaledHeight;
} else {
width = downscaledWidth;
}
} else {
if (originalWidth < originalHeight) {
width = downscaledWidth;
} else if (originalHeight < originalWidth) {
height = downscaledHeight;
}
}
}
return Size(width, height);
double widthFactor = maxWidth != null ? imageSize.width / maxWidth : 1;
double heightFactor = maxHeight != null ? imageSize.height / maxHeight : 1;
double resizeFactor = max(widthFactor, heightFactor);
return (resizeFactor > 1 ? imageSize ~/ resizeFactor : imageSize);
}