-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read image dimensions #1297
Comments
Related: It would be also nice to add the image dimensions as a restriction. |
Indeed uppy doesn't have these built in. It would be possible to do with a plugin. For the Blob/FileReader dance, the easier thing to do is to use URL.createObjectURL, which saves a few steps: uppy.on('file-added', (file) => {
const data = file.data // is a Blob instance
const url = URL.createObjectURL(data)
const image = new Image()
image.src = url
image.onload = () => {
uppy.setFileMeta(file.id, { width: image.width, height: image.height })
URL.revokeObjectURL(url)
}
}) As for the const image = new Image()
image.src = file.preview
image.onload = () => { ... } Then you do not need the URL.createObjectURL and URL.revokeObjectURL calls. |
Thanks @goto-bus-stop for the comprehensive solution! |
How to achieve this for a remote file eg Facebook or Intagram? |
@RohiniJindam Unfortunately that is not possible, since there is no way to download remote files to the browser. |
@goto-bus-stop thanks for the quick reply.
Do you see any issues here? |
That kinda works, but note that the |
So preprocessors also cannot work for remote files? This plugin for example Image Processor Plugin @goto-bus-stop |
Just going to leave this here in case helpful to anyone. I wanted to check the aspect ratio of the uploaded image and reject it if it was too weird. Using some of the logic above, I ended up with something like the below. The main assumption here is that the image preview doesn't change the underlying dimensions (or at least their ratio) of the uploaded image. The only meh part of this is that you get a flash of the thumbnail and then it's immediately removed. uppy.on('thumbnail:generated', (file, preview) => {
const img = new Image();
img.src = preview;
img.onload = () => {
const aspect_ratio = img.width / img.height;
if (aspect_ratio > 1.8 || aspect_ratio < 0.5625 ) {
uppy.removeFile(file.id);
uppy.info(
'Aspect ratio for photo is too skewed, please fix and try again.');
}
}
}); |
Hello, and thanks for the great lib!
Is there a way to access the image dimensions within Uppy without need to go through a Blob -> FileReader -> Image conversion? I was unable to find a
width
/height
metadata fields on the objects returned by theupload-success
andcomplete
events.I'm currently doing this like so:
The text was updated successfully, but these errors were encountered: