diff --git a/src/CONST.js b/src/CONST.js index ef02585f6aed..a671800f5860 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -32,6 +32,11 @@ const CONST = { // Minimum width and height size in px for a selected image AVATAR_MIN_WIDTH_PX: 80, AVATAR_MIN_HEIGHT_PX: 80, + + // Maximum width and height size in px for a selected image + AVATAR_MAX_WIDTH_PX: 4096, + AVATAR_MAX_HEIGHT_PX: 4096, + NEW_EXPENSIFY_URL: ACTIVE_EXPENSIFY_URL, APP_DOWNLOAD_LINKS: { ANDROID: `https://play.google.com/store/apps/details?id=${ANDROID_PACKAGE_NAME}`, diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 418be07b819f..6944f75fd2cf 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -74,6 +74,8 @@ function getDataForUpload(fileData) { const fileResult = { name: fileName, type: fileData.type, + width: fileData.width, + height: fileData.height, uri: fileData.fileCopyUri || fileData.uri, size: fileData.fileSize || fileData.size, }; diff --git a/src/components/AvatarWithImagePicker.js b/src/components/AvatarWithImagePicker.js index 4d47a0f405e8..7065f23aede3 100644 --- a/src/components/AvatarWithImagePicker.js +++ b/src/components/AvatarWithImagePicker.js @@ -1,8 +1,6 @@ import _ from 'underscore'; import React from 'react'; -import { - Pressable, View, Image, -} from 'react-native'; +import {Pressable, View} from 'react-native'; import PropTypes from 'prop-types'; import lodashGet from 'lodash/get'; import Avatar from './Avatar'; @@ -21,6 +19,7 @@ import SpinningIndicatorAnimation from '../styles/animation/SpinningIndicatorAni import Tooltip from './Tooltip'; import stylePropTypes from '../styles/stylePropTypes'; import * as FileUtils from '../libs/fileDownload/FileUtils'; +import getImageResolution from '../libs/fileDownload/getImageResolution'; const propTypes = { /** Avatar URL to display */ @@ -148,17 +147,16 @@ class AvatarWithImagePicker extends React.Component { } /** - * Check if the attachment resolution is bigger than required. + * Check if the attachment resolution matches constraints. * - * @param {String} imageUri + * @param {Object} image * @returns {Promise} */ - isValidResolution(imageUri) { - return new Promise((resolve) => { - Image.getSize(imageUri, (width, height) => { - resolve(height >= CONST.AVATAR_MIN_HEIGHT_PX && width >= CONST.AVATAR_MIN_WIDTH_PX); - }); - }); + isValidResolution(image) { + return getImageResolution(image).then(resolution => ( + (resolution.height >= CONST.AVATAR_MIN_HEIGHT_PX && resolution.width >= CONST.AVATAR_MIN_WIDTH_PX) + && (resolution.height <= CONST.AVATAR_MAX_HEIGHT_PX && resolution.width <= CONST.AVATAR_MAX_WIDTH_PX) + )); } /** @@ -182,14 +180,16 @@ class AvatarWithImagePicker extends React.Component { return; } - this.isValidResolution(image.uri) + this.isValidResolution(image) .then((isValidResolution) => { if (!isValidResolution) { this.showErrorModal( this.props.translate('avatarWithImagePicker.imageUploadFailed'), - this.props.translate('avatarWithImagePicker.tooSmallResolution', { + this.props.translate('avatarWithImagePicker.resolutionConstraints', { minHeightInPx: CONST.AVATAR_MIN_HEIGHT_PX, minWidthInPx: CONST.AVATAR_MIN_WIDTH_PX, + maxHeightInPx: CONST.AVATAR_MAX_HEIGHT_PX, + maxWidthInPx: CONST.AVATAR_MAX_WIDTH_PX, }), ); return; diff --git a/src/languages/en.js b/src/languages/en.js index d849ce04ee14..83f897fb3b6c 100755 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -284,7 +284,9 @@ export default { imageUploadFailed: 'Image upload failed', deleteWorkspaceError: 'Sorry, there was an unexpected problem deleting your workspace avatar.', sizeExceeded: ({maxUploadSizeInMB}) => `The selected image exceeds the maximum upload size of ${maxUploadSizeInMB}MB.`, - tooSmallResolution: ({minHeightInPx, minWidthInPx}) => `Please upload an image larger than ${minHeightInPx}x${minWidthInPx} pixels`, + resolutionConstraints: ({ + minHeightInPx, minWidthInPx, maxHeightInPx, maxWidthInPx, + }) => `Please upload an image larger than ${minHeightInPx}x${minWidthInPx} pixels and smaller than ${maxHeightInPx}x${maxWidthInPx} pixels`, notAllowedExtension: ({allowedExtensions}) => `Profile picture must be one of the following types: ${allowedExtensions.join(', ')}.`, }, profilePage: { diff --git a/src/languages/es.js b/src/languages/es.js index 889b0ab8c6bf..3e77b4c059b3 100644 --- a/src/languages/es.js +++ b/src/languages/es.js @@ -284,7 +284,9 @@ export default { imageUploadFailed: 'Error al cargar la imagen', deleteWorkspaceError: 'Lo sentimos, hubo un problema eliminando el avatar de su espacio de trabajo.', sizeExceeded: ({maxUploadSizeInMB}) => `La imagen supera el tamaño máximo de ${maxUploadSizeInMB}MB.`, - tooSmallResolution: ({minHeightInPx, minWidthInPx}) => `Por favor elige una imagen mas grande que ${minHeightInPx}x${minWidthInPx} píxeles`, + resolutionConstraints: ({ + minHeightInPx, minWidthInPx, maxHeightInPx, maxWidthInPx, + }) => `Por favor, elige una imagen más grande que ${minHeightInPx}x${minWidthInPx} píxeles y más pequeña que ${maxHeightInPx}x${maxWidthInPx} píxeles`, notAllowedExtension: ({allowedExtensions}) => `La foto de perfil debe ser de uno de los siguientes tipos: ${allowedExtensions.join(', ')}.`, }, profilePage: { diff --git a/src/libs/fileDownload/getImageResolution.js b/src/libs/fileDownload/getImageResolution.js new file mode 100644 index 000000000000..2f9a6d4fbdb4 --- /dev/null +++ b/src/libs/fileDownload/getImageResolution.js @@ -0,0 +1,33 @@ +/** + * Get image resolution + * File object is returned as a result of a user selecting image using the + * We need to create a new Image object and get dimensions from it + * Opposite to native where we already have width and height properties coming from library + * + * new Image() is used specifically for performance reasons, opposed to using FileReader (5ms vs +100ms) + * because FileReader is slow and causes a noticeable delay in the UI when selecting an image. + * + * @param {*} file Picked file blob + * @returns {Promise} + */ +function getImageResolution(file) { + if (!(file instanceof File)) { + return Promise.reject(new Error('Object is not an instance of File')); + } + + return new Promise((resolve, reject) => { + const image = new Image(); + const objectUrl = URL.createObjectURL(file); + image.onload = function () { + resolve({ + width: this.naturalWidth, + height: this.naturalHeight, + }); + URL.revokeObjectURL(objectUrl); + }; + image.onerror = reject; + image.src = objectUrl; + }); +} + +export default getImageResolution; diff --git a/src/libs/fileDownload/getImageResolution.native.js b/src/libs/fileDownload/getImageResolution.native.js new file mode 100644 index 000000000000..f291886f4665 --- /dev/null +++ b/src/libs/fileDownload/getImageResolution.native.js @@ -0,0 +1,14 @@ +/** + * Get image resolution + * Image object is returned as a result of a user selecting image using the react-native-image-picker + * Image already has width and height properties coming from library so we just need to return them on native + * Opposite to web where we need to create a new Image object and get dimensions from it + * + * @param {*} file Picked file blob + * @returns {Promise} + */ +function getImageResolution(file) { + return Promise.resolve({width: file.width, height: file.height}); +} + +export default getImageResolution;