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

[image_picker_for_web] Added image resize functionality #3439

Closed
wants to merge 4 commits into from

Conversation

jesusrp98
Copy link

@jesusrp98 jesusrp98 commented Jan 17, 2021

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

  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@google-cla
Copy link

google-cla bot commented Jan 17, 2021

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 @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Jan 17, 2021
@jesusrp98
Copy link
Author

@googlebot I signed it!

@google-cla google-cla bot added cla: yes and removed cla: no labels Jan 17, 2021
Copy link
Member

@ditman ditman left a 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?

int imageQuality,
) {
final completer = Completer<String>();
final img = html.ImageElement();
Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

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 :)

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I understand what you're saying here, but I don't know how to exactly add this element to the DOM tree :/

I'd need some help here @postacik @ditman .

ratio = maxWidth / img.height;
}
if (img.width > img.height) {
if (img.width < maxHeight) ratio = maxHeight / img.width;
Copy link
Member

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 (?)

Copy link
Author

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.

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.

Copy link
Author

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.

Comment on lines +90 to +92
// Flush cache
img.src =
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
Copy link
Member

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)

Copy link
Author

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 (?).

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.

Copy link
Author

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!

@jesusrp98
Copy link
Author

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 :)

@jesusrp98 jesusrp98 requested a review from ditman January 22, 2021 14:11
@ditman
Copy link
Member

ditman commented Jan 23, 2021

Haha thanks for the clarification @jesusrp98! I didn't realize this was a Draft PR 🤦 :P

@Hixie
Copy link
Contributor

Hixie commented Oct 12, 2021

@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!

@ditman
Copy link
Member

ditman commented Oct 14, 2021

Coincidentally, this is being worked on here as of late: #4389

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants