Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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 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
fix calculate size logic
  • Loading branch information
balvinderz committed Sep 30, 2021
commit 91f92e65b91f9b050190b2c168fea2c62131b49a
88 changes: 58 additions & 30 deletions packages/image_picker/image_picker_for_web/lib/image_resizer.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import 'dart:async';
import 'dart:math';
import 'dart:ui';

import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
import 'dart:html' as html;


/// Resizes images
class ImageResizer {
/// Resizes images if needed
Future<XFile> resizeImageIfNeeded(XFile file, double? maxWidth,
double? maxHeight, int? imageQuality) async {
if (maxWidth == null && maxHeight == null && imageQuality == null ||
if (maxWidth == null &&
maxHeight == null &&
_isImageQualityValid(imageQuality) &&
file.mimeType == "image/gif") {
//TODO Implement maxWidth and maxHeight for image/gif
return file;
}
final imageLoadCompleter = Completer();
Expand All @@ -22,16 +25,10 @@ class ImageResizer {
imageLoadCompleter.complete();
});

//Return the original image if error comes
imageElement.onError.listen((event) {
imageLoadCompleter.complete();
});
await imageLoadCompleter.future;

final newImageSize = calculateSize(
imageElement.width!.toDouble(), imageElement.height!.toDouble(),
maxWidth ?? imageElement.width!.toDouble(),
maxHeight ?? imageElement.height!.toDouble());
final newImageSize = calculateSize(imageElement.width!.toDouble(),
imageElement.height!.toDouble(), maxWidth, maxHeight);
final canvas = html.CanvasElement();
canvas.width = newImageSize.width.toInt();
canvas.height = newImageSize.height.toInt();
Expand All @@ -44,34 +41,65 @@ class ImageResizer {
}
final blob = await canvas.toBlob(
file.mimeType,
(imageQuality ?? 100) /
(min(imageQuality ?? 100, 100)) /
100.0); // Image quality only works for jpeg images
return XFile(
html.Url.createObjectUrlFromBlob(blob),
return XFile(html.Url.createObjectUrlFromBlob(blob),
mimeType: file.mimeType,
name: file.name,
lastModified: DateTime.now(),
length: blob.size
);
length: blob.size);
}


/// Calculates the size of the scaled image.
Size calculateSize(double imageWidth, double imageHeight, double maxWidth,
double maxHeight) {
var width = imageWidth;
var height = imageHeight;
var scaledHeight = height;
var scaledWidth = width;
if (width > height) {
if (width > maxWidth) {
scaledHeight = ((height * maxWidth) / width);
}
} else {
if (height > maxHeight) {
scaledWidth = ((width * maxHeight) / height);
Size calculateSize(double imageWidth, double imageHeight, double? maxWidth,
double? maxHeight) {
double originalWidth = imageWidth;
double originalHeight = imageHeight;

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(scaledWidth, scaledHeight);
if (hasMaxHeight) {
assert(height <= maxHeight);
}
if (hasMaxWidth) {
assert(width <= maxWidth);
}
return Size(width, height);
}

bool _isImageQualityValid(int? imageQuality) {
return imageQuality == null || (imageQuality >= 0 && imageQuality <= 100);
}
}