This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[image_picker_for_web] Added support for maxWidth, maxHeight and imageQuality #4389
Merged
fluttergithubbot
merged 37 commits into
flutter:master
from
balvinderz:compress_file_web
Oct 18, 2021
Merged
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 c3c1344
image scaling and quality done
balvinderz cf0ee4d
implement max width max height and imageQuality for web
balvinderz 130f78a
added maxWidth max height and imageQuality for web
balvinderz b8d3d65
dartfmt
balvinderz be720c2
revert pubspec.yaml of image_picker
balvinderz 754cec5
run format
balvinderz 942ac46
added image resizer class
balvinderz 982d40a
Merge branch 'master' of https://github.com/flutter/plugins into comp…
balvinderz 6145cf5
Merge branch 'compress_file_web' of https://github.com/balvinderz/plu…
balvinderz 91f92e6
fix calculate size logic
balvinderz b10408f
fix if condition in resizeImage
balvinderz 42d8191
image resizer done
balvinderz 019a738
added utils test'
balvinderz 2b1842d
update readme
balvinderz 5f7a0dd
add image resizer tests
balvinderz f61b965
dartfmt
balvinderz 5b477e6
add licenses
balvinderz 60e0b9f
remove path dependancy
balvinderz 7a63323
fix typo and remove extra space
balvinderz c75b036
fix doc comment
balvinderz f5e6d45
remove double spaces
balvinderz d38de67
review changes
balvinderz 5c352f6
fix typo
balvinderz 08b8240
use completeError
balvinderz 7151d71
remove unused imports
balvinderz a3bbc58
dartfmt
balvinderz d81990b
fix typo
balvinderz c90eff3
remove 1 doc comment
balvinderz 811b3fb
update tests and resizing algorithm
balvinderz 3e4d7c1
Review changes
balvinderz 554e996
move image resizer files in src
balvinderz d15b8b1
dartfmt
balvinderz b87f0cf
dartfmt again
balvinderz d99a702
Fix some review comments
ditman 5e825ab
Slight update in the CHANGELOG
ditman c4d057e
Merge branch 'master' into compress_file_web
ditman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added utils test'
- Loading branch information
commit 019a738ce899fca3b4a722d4c710c6027040713c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/image_picker/image_picker_for_web/lib/image_resizer_utils.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import 'dart:math'; | ||
|
|
||
| import 'dart:ui'; | ||
|
|
||
| ///a function that checks if an image needs to be resized or not | ||
| bool imageResizeNeeded(double? maxWidth, double? maxHeight, int? imageQuality) { | ||
| bool resizeNeeded = | ||
| (maxWidth != null || maxHeight != null || imageQuality != null); | ||
| if (resizeNeeded) { | ||
| if (imageQuality != null) { | ||
| return isImageQualityValid(imageQuality); | ||
| } else { | ||
| return true; | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
balvinderz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// a function that checks if image quality is between [0,100] or null | ||
| bool isImageQualityValid(int imageQuality) { | ||
| return (imageQuality >= 0 && imageQuality <= 100); | ||
| } | ||
|
|
||
| /// a functions that calculates the size of the scaled image. | ||
| /// imageWidth is the width of the image | ||
| /// imageHeight is the height of the image | ||
| /// maxWidth is the maximum width of the scaled image | ||
| /// maxHeight is the maximum height of the scaled image | ||
| Size calculateSizeOfScaledImage(double imageWidth, double imageHeight, | ||
balvinderz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| double? maxWidth, double? maxHeight) { | ||
ditman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| double originalWidth = imageWidth; | ||
| double originalHeight = imageHeight; | ||
|
|
||
| bool hasMaxWidth = maxWidth != null; | ||
balvinderz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
packages/image_picker/image_picker_for_web/test/image_resizer_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import 'dart:math'; | ||
| import 'dart:ui'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:image_picker_for_web/image_resizer_utils.dart'; | ||
|
|
||
| void main() { | ||
| group('Image Resizer Utils', () { | ||
| group("calculateSizeOfScaledImage", () { | ||
| test( | ||
| "scaled image height and width are same if max width and max height are same as image's width and height ", | ||
| () { | ||
| expect(calculateSizeOfScaledImage(500, 300, 500, 300), Size(500, 300)); | ||
| }); | ||
| test( | ||
| "scaled image height and width are same if max width and max height are null", | ||
| () { | ||
| expect( | ||
| calculateSizeOfScaledImage(500, 300, null, null), Size(500, 300)); | ||
| }); | ||
| test("image size is scaled when maxWidth is set", () { | ||
| final imageSize = Size(500, 300); | ||
| final maxWidth = 400; | ||
| final scaledSize = calculateSizeOfScaledImage( | ||
| imageSize.width, imageSize.height, maxWidth.toDouble(), null); | ||
| expect(scaledSize.height<=imageSize.height,true); | ||
| expect(scaledSize.width<=maxWidth,true); | ||
|
|
||
| }); | ||
| test("image size is scaled when maxHeight is set", () { | ||
| final imageSize = Size(500, 300); | ||
| final maxHeight = 400; | ||
| final scaledSize = calculateSizeOfScaledImage( | ||
| imageSize.width, imageSize.height, null,maxHeight.toDouble()); | ||
| expect(scaledSize.height<=maxHeight,true); | ||
| expect(scaledSize.width<=imageSize.width,true); | ||
|
|
||
| }); | ||
| test("image size is scaled when both maxWidth and maxHeight is set", () { | ||
| final imageSize = Size(1120, 2000); | ||
| final maxHeight = 1200; | ||
| final maxWidth = 99; | ||
| final scaledSize = calculateSizeOfScaledImage( | ||
| imageSize.width, imageSize.height, maxWidth.toDouble(),maxHeight.toDouble()); | ||
| expect(scaledSize.height<=maxHeight,true); | ||
| expect(scaledSize.width<=maxWidth,true); | ||
|
|
||
| }); | ||
| }); | ||
| group("imageResizeNeeded", () { | ||
| test("image needs to be resized when maxWidth is set", () { | ||
| expect(imageResizeNeeded(50, null, null), true); | ||
| }); | ||
| test("image needs to be resized when maxHeight is set", () { | ||
| expect(imageResizeNeeded(null, 50, null), true); | ||
| }); | ||
| test("image needs to be resized when imageQuality is set", () { | ||
| expect(imageResizeNeeded(null, null, 100), true); | ||
| }); | ||
| test("image will not be resized when imageQuality is not valid", () { | ||
| expect(imageResizeNeeded(null, null, 101), false); | ||
| expect(imageResizeNeeded(null, null, -1), false); | ||
| }); | ||
| }); | ||
| group("isImageQualityValid", () { | ||
| test("image quality is valid in 0 to 100", () { | ||
| expect(isImageQualityValid(50), true); | ||
| expect(isImageQualityValid(0), true); | ||
| expect(isImageQualityValid(100), true); | ||
| }); | ||
| test( | ||
| "image quality is not valid when imageQuality is less than 0 or greater than 100", | ||
| () { | ||
| expect(isImageQualityValid(-1), false); | ||
| expect(isImageQualityValid(101), false); | ||
| }); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.