-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[image_picker_for_web] Added image resize functionality #3439
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs some tests (here), also, I made a couple of comments in the general approach.
Have you verified this works with the example
app of the plugin in web?
packages/image_picker/image_picker_for_web/lib/image_picker_for_web.dart
Outdated
Show resolved
Hide resolved
packages/image_picker/image_picker_for_web/lib/image_picker_for_web.dart
Outdated
Show resolved
Hide resolved
int imageQuality, | ||
) { | ||
final completer = Completer<String>(); | ||
final img = html.ImageElement(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need to add this img
to the DOM for the events to fire?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm haven't had many experience of developing flutter web plugins and I don't know how to proceed here.
Could you elaborate on this @ditman please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, some times for some DOM elements to fire their load/error events they must be injected into the markup of the page (anywhere, but they need to be injected).
I'm not sure this is the case, but I'd test this code across all main browsers (Edge/Safari/Firefox/Chrome) to ensure that the on*** events of the ImageElement are firing as expected.
Same approach for the comment below where we're resetting the image to something blank, and then the desired source again :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ditman , you are right, events are generally fired if the element is added to DOM tree but while converting my JavaScript code to Dart, I realized that it worked without adding it to DOM for Chrome. I haven't tested for other mentioned browsers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ratio = maxWidth / img.height; | ||
} | ||
if (img.width > img.height) { | ||
if (img.width < maxHeight) ratio = maxHeight / img.width; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is odd, comparing a width to a height. I think there's some typos in this algo to determine the aspect ratio / scale factor (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look a the proper algorithm here, maybe I can refine this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ditman , the scaling algorithm was just a copy paste from an old JavaScript project. I really don't recall how I constructed it or tested it thoroughly.
The best algorithm for the resize function would be using the algorithm used in the mobile counter part. Maybe the algorithm may be copied from the following Java file so that it will be consistent with the mobile version.
Line 56 in 6783cd5
private File resizedImage( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you @postacik. I'll update the current algorith to resemble something similar to the Android implementation.
// Flush cache | ||
img.src = | ||
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=='; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this trigger the onLoad event above? If so, you might want to check for this special image to do nothing with it; otherwise you'll attempt to complete the completer
several times, and it'll throw an exception (you can only complete a completer once)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's a bit weird here and I don't really understand what's going on.
Maybe @postacik could help us here (?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ditman, you are right, if it does trigger the onload event several times, it would cause exceptions. The JavaScript code I used was not using a Promise (or Completer) but simply copying the result to the canvas so the hack to avoid cases where onload did not fire did not have any side effects but here it would. The base64 image is a 1x1 gif. Maybe the completer may be avoided if the loaded image size is 1x1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a simple img.width > 1 && img.height > 1
check before performing the resize logic.
Give me your feedback here!
Sorry @ditman. The first commit was just copying and pasting the resize function to the code base. I'll continue with the work now and implement all your feedback. I'm really thankful for that because I haven't really tinkered with flutter web plugins before :) |
Haha thanks for the clarification @jesusrp98! I didn't realize this was a Draft PR 🤦 :P |
@jesusrp98 I'm going to close this PR for now since it's not ready for landing but please don't hesitate to reopen it (or submit a new one) if you are still working on this. Thanks! |
Coincidentally, this is being worked on here as of late: #4389 |
Added image resize functionality to the web implementation of
image_picker
to make it par with the mobile implementation of the plugin.This implementation comes from @postacik, detailed here on #2802.
Huge thanks to @ditman as well for all the help!
This SHOULD NOT be a breaking change.
Also this is my first PR here so bare with me :)
Pre-launch Checklist
[shared_preferences]
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.