Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[image_picker_for_web] Added support for maxWidth, maxHeight and imageQuality #4389

Merged
merged 37 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_for_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.4

* Implemented `maxWidth`, `maxHeight` and `imageQuality`

## 2.1.3

* Add `implements` to pubspec.
Expand Down
124 changes: 104 additions & 20 deletions packages/image_picker/image_picker_for_web/lib/image_picker_for_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class ImagePickerPlugin extends ImagePickerPlatform {
List<XFile> files = await getFiles(
accept: _kAcceptImageMimeType,
capture: capture,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
);
return files.first;
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -158,7 +161,12 @@ class ImagePickerPlugin extends ImagePickerPlatform {
double? maxHeight,
int? imageQuality,
}) {
return getFiles(accept: _kAcceptImageMimeType, multiple: true);
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
return getFiles(
accept: _kAcceptImageMimeType,
multiple: true,
imageQuality: imageQuality,
maxHeight: maxHeight,
maxWidth: maxWidth);
}

/// Injects a file input with the specified accept+capture attributes, and
Expand All @@ -171,19 +179,22 @@ class ImagePickerPlugin extends ImagePickerPlatform {
///
/// See https://caniuse.com/#feat=html-media-capture
@visibleForTesting
Future<List<XFile>> getFiles({
String? accept,
String? capture,
bool multiple = false,
}) {
Future<List<XFile>> getFiles(
{String? accept,
String? capture,
bool multiple = false,
double? maxWidth,
double? maxHeight,
int? imageQuality}) {
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
html.FileUploadInputElement input = createInputElement(
accept,
capture,
multiple: multiple,
) as html.FileUploadInputElement;
_injectAndActivate(input);

return _getSelectedXFiles(input);
return _getSelectedXFiles(input,
maxWidth: maxWidth, maxHeight: maxHeight, imageQuality: imageQuality);
}

// DOM methods
Expand Down Expand Up @@ -238,23 +249,29 @@ class ImagePickerPlugin extends ImagePickerPlatform {
}

/// Monitors an <input type="file"> and returns the selected file(s).
Future<List<XFile>> _getSelectedXFiles(html.FileUploadInputElement input) {
Future<List<XFile>> _getSelectedXFiles(html.FileUploadInputElement input,
{double? maxWidth, double? maxHeight, int? imageQuality}) {
final Completer<List<XFile>> _completer = Completer<List<XFile>>();
// Observe the input until we can return something
input.onChange.first.then((event) {
input.onChange.first.then((event) async {
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
final files = _handleOnChangeEvent(event);
if (!_completer.isCompleted && files != null) {
_completer.complete(files
.map((file) => XFile(
html.Url.createObjectUrl(file),
name: file.name,
length: file.size,
lastModified: DateTime.fromMillisecondsSinceEpoch(
file.lastModified ?? DateTime.now().millisecondsSinceEpoch,
),
mimeType: file.type,
))
.toList());
//Convert the file if one of the parameter is not null
if (imageQuality != null || maxWidth != null || maxHeight != null) {
final convertedFileUris = await Future.wait(files.map((e) async =>
await _getCompressedUri(e, imageQuality, maxHeight, maxWidth)));
balvinderz marked this conversation as resolved.
Show resolved Hide resolved

int index = 0;
_completer.complete(files.map((file) {
index += 1;
final xFile = file.toXFile(path: convertedFileUris[index - 1]);
return xFile;
}).toList());
} else {
_completer.complete(files.map((file) {
return file.toXFile();
}).toList());
}
}
});
input.onError.first.then((event) {
Expand Down Expand Up @@ -310,6 +327,73 @@ class ImagePickerPlugin extends ImagePickerPlatform {
_target.children.add(element);
element.click();
}

Future<String> _getCompressedUri(html.File file, int? imageQuality,
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
double? maxHeight, double? maxWidth) async {
//max width, max height, imageQuality are not supported for gif
if (file.type == "image/gif") {
return html.Url.createObjectUrl(file);
}
final Completer<String> _completer = Completer<String>();
final blobUrl = html.Url.createObjectUrl(file);
final image = html.ImageElement(src: blobUrl);
image.onLoad.listen((event) async {
html.Url.revokeObjectUrl(blobUrl);
final canvas = html.CanvasElement();
final size = _calculateSize(image, maxWidth ?? image.width!.toDouble(),
maxHeight ?? image.height!.toDouble());
canvas.width = size[0];
canvas.height = size[1];
final ctx = canvas.context2D;
if (maxHeight == null && maxWidth == null) {
ctx.drawImage(image, 0, 0);
} else {
ctx.drawImageScaled(image, 0, 0, canvas.width!, canvas.height!);
}
final blob = await canvas.toBlob(
file.type,
(imageQuality ?? 100) /
100.0); // Image quality only works for jpeg images
_completer.complete(html.Url.createObjectUrlFromBlob(blob));
});
image.onError.listen((event) {
//Return the original image if error comes
_completer.complete(html.Url.createObjectUrl(file));
});
return _completer.future;
}

List<int> _calculateSize(
html.ImageElement img, double maxWidth, double maxHeight) {
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
var width = img.width!;
var height = img.height!;

// calculate the width and height, constraining the proportions
if (width > height) {
if (width > maxWidth) {
height = ((height * maxWidth) / width).round();
width = maxWidth.toInt();
}
} else {
if (height > maxHeight) {
width = ((width * maxHeight) / height).round();
height = maxHeight.toInt();
}
}
return [width, height];
}
}

extension on html.File {
XFile toXFile({String? path}) => XFile(
path ?? html.Url.createObjectUrl(this),
name: this.name,
length: this.size,
lastModified: DateTime.fromMillisecondsSinceEpoch(
this.lastModified ?? DateTime.now().millisecondsSinceEpoch,
),
mimeType: this.type,
);
balvinderz marked this conversation as resolved.
Show resolved Hide resolved
}

// Some tools to override behavior for unit-testing
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_for_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: image_picker_for_web
description: Web platform implementation of image_picker
repository: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker_for_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 2.1.3
version: 2.1.4

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down