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
review changes
  • Loading branch information
balvinderz committed Oct 2, 2021
commit d38de67aed16653069b768c6eebb4e7e706a588c
59 changes: 38 additions & 21 deletions packages/image_picker/image_picker_for_web/lib/image_resizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:math';
import 'dart:ui';
import 'package:image_picker_for_web/image_resizer_utils.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
import 'dart:html' as html;
Expand All @@ -19,44 +20,60 @@ class ImageResizer {
//TODO Implement maxWidth and maxHeight for image/gif
return file;
}
final imageLoadCompleter = Completer();
final imageElement = await loadImage(file.path);
if (imageElement == null) {
return file;
}
final canvas = resizeImageElement(imageElement, maxWidth, maxHeight);
return writeCanvasToFile(file, canvas, imageQuality);
}

/// function that loads the blobUrl into an imageElement
/// returns null if error comes while loading the image
Future<html.ImageElement?> loadImage(String blobUrl) {
final imageLoadCompleter = Completer<html.ImageElement?>();
final imageElement = html.ImageElement();
imageElement.src = file.path;
bool hasError = false;
imageElement.src = blobUrl;

imageElement.onLoad.listen((event) {
html.Url.revokeObjectUrl(file.path);
imageLoadCompleter.complete();
html.Url.revokeObjectUrl(blobUrl);
imageLoadCompleter.complete(imageElement);
});
imageElement.onError.listen((event) {
imageLoadCompleter.complete();
hasError = true;
imageLoadCompleter.complete(null);
});
await imageLoadCompleter.future;
if (hasError) {
return file;
}
final newImageSize = calculateSizeOfScaledImage(
imageElement.width!.toDouble(),
imageElement.height!.toDouble(),
return imageLoadCompleter.future;
}

/// Draws image to a canvas while resizing the image to fit the [maxWidth],[maxHeight] constraints
html.CanvasElement resizeImageElement(
html.ImageElement source, double? maxWidth, double? maxHeight) {
final newImageSize = calculateSizeOfDownScaledImage(
Size(source.width!.toDouble(), source.height!.toDouble()),
maxWidth,
maxHeight);
final canvas = html.CanvasElement();
canvas.width = newImageSize.width.toInt();
canvas.height = newImageSize.height.toInt();
final context = canvas.context2D;
if (maxHeight == null && maxWidth == null) {
context.drawImage(imageElement, 0, 0);
context.drawImage(source, 0, 0);
} else {
context.drawImageScaled(
imageElement, 0, 0, canvas.width!, canvas.height!);
context.drawImageScaled(source, 0, 0, canvas.width!, canvas.height!);
}
return canvas;
}

/// function that converts a canvas element to Xfile
/// [imageQuality] is only supported for jpeg and webp images.
Future<XFile> writeCanvasToFile(
XFile originalFile, html.CanvasElement canvas, int? imageQuality) async {
final calculatedImageQuality = ((min(imageQuality ?? 100, 100)) / 100.0);
final blob = await canvas.toBlob(file.mimeType,
calculatedImageQuality); // Image quality only works for jpeg and webp images
final blob =
await canvas.toBlob(originalFile.mimeType, calculatedImageQuality);
return XFile(html.Url.createObjectUrlFromBlob(blob),
mimeType: file.mimeType,
name: "scaled_" + file.name,
mimeType: originalFile.mimeType,
name: "scaled_" + originalFile.name,
lastModified: DateTime.now(),
length: blob.size);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,25 @@ 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;
}
return imageQuality != null
? isImageQualityValid(imageQuality)
: (maxWidth != null || maxHeight != null);
}

/// a function that checks if image quality is between 0 to 100
bool isImageQualityValid(int imageQuality) {
return (imageQuality >= 0 && imageQuality <= 100);
}

/// a functions that calculates the size of the scaled image.
/// a functions that calculates the size of the downScaled 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,
double? maxWidth, double? maxHeight) {
double originalWidth = imageWidth;
double originalHeight = imageHeight;
Size calculateSizeOfDownScaledImage(
Size imageSize, double? maxWidth, double? maxHeight) {
double originalWidth = imageSize.width;
double originalHeight = imageSize.height;

bool hasMaxWidth = maxWidth != null;
bool hasMaxHeight = maxHeight != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@ void main() {
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));
expect(calculateSizeOfDownScaledImage(Size(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));
expect(calculateSizeOfDownScaledImage(Size(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);
final scaledSize = calculateSizeOfDownScaledImage(
Size(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());
final scaledSize = calculateSizeOfDownScaledImage(
Size(imageSize.width, imageSize.height),
null,
maxHeight.toDouble());
expect(scaledSize.height <= maxHeight, true);
expect(scaledSize.width <= imageSize.width, true);
});
Expand All @@ -44,8 +47,10 @@ void main() {
final imageSize = Size(1120, 2000);
final maxHeight = 1200;
final maxWidth = 99;
final scaledSize = calculateSizeOfScaledImage(imageSize.width,
imageSize.height, maxWidth.toDouble(), maxHeight.toDouble());
final scaledSize = calculateSizeOfDownScaledImage(
Size(imageSize.width, imageSize.height),
maxWidth.toDouble(),
maxHeight.toDouble());
expect(scaledSize.height <= maxHeight, true);
expect(scaledSize.width <= maxWidth, true);
});
Expand Down