feat: Compress avatar images before upload (#1751)
* compress avatar images before upload * move compressImage to dedicated file * Update ImageUpload.js
This commit is contained in:
parent
b6ab816bb3
commit
89903b4bbe
|
@ -10,6 +10,7 @@ import Button from "components/Button";
|
|||
import Flex from "components/Flex";
|
||||
import LoadingIndicator from "components/LoadingIndicator";
|
||||
import Modal from "components/Modal";
|
||||
import { compressImage } from "utils/compressImage";
|
||||
import { uploadFile, dataUrlToBlob } from "utils/uploadFile";
|
||||
|
||||
const EMPTY_OBJECT = {};
|
||||
|
@ -53,7 +54,11 @@ class ImageUpload extends React.Component<Props> {
|
|||
const canvas = this.avatarEditorRef.getImage();
|
||||
const imageBlob = dataUrlToBlob(canvas.toDataURL());
|
||||
try {
|
||||
const attachment = await uploadFile(imageBlob, {
|
||||
const compressed = await compressImage(imageBlob, {
|
||||
maxHeight: 512,
|
||||
maxWidth: 512,
|
||||
});
|
||||
const attachment = await uploadFile(compressed, {
|
||||
name: this.file.name,
|
||||
public: true,
|
||||
});
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
// @flow
|
||||
import Compressor from "compressorjs";
|
||||
|
||||
type Options = Omit<Compressor.Options, "success" | "error">;
|
||||
|
||||
export const compressImage = async (
|
||||
file: File | Blob,
|
||||
options?: Options
|
||||
): Promise<Blob> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
new Compressor(file, {
|
||||
...options,
|
||||
success: resolve,
|
||||
error: reject,
|
||||
});
|
||||
});
|
||||
};
|
|
@ -80,6 +80,7 @@
|
|||
"boundless-popover": "^1.0.4",
|
||||
"bull": "^3.5.2",
|
||||
"cancan": "3.1.0",
|
||||
"compressorjs": "^1.0.7",
|
||||
"copy-to-clipboard": "^3.0.6",
|
||||
"core-js": "2",
|
||||
"date-fns": "1.29.0",
|
||||
|
|
18
yarn.lock
18
yarn.lock
|
@ -2589,6 +2589,11 @@ bluebird@~3.4.0, bluebird@~3.4.1:
|
|||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.7.tgz#f72d760be09b7f76d08ed8fae98b289a8d05fab3"
|
||||
integrity sha1-9y12C+Cbf3bQjtj66Ysomo0F+rM=
|
||||
|
||||
blueimp-canvas-to-blob@^3.28.0:
|
||||
version "3.28.0"
|
||||
resolved "https://registry.yarnpkg.com/blueimp-canvas-to-blob/-/blueimp-canvas-to-blob-3.28.0.tgz#c8ab4dc6bb08774a7f273798cdf94b0776adf6c8"
|
||||
integrity sha512-5q+YHzgGsuHQ01iouGgJaPJXod2AzTxJXmVv90PpGrRxU7G7IqgPqWXz+PBmt3520jKKi6irWbNV87DicEa7wg==
|
||||
|
||||
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.4.0:
|
||||
version "4.11.9"
|
||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.9.tgz#26d556829458f9d1e81fc48952493d0ba3507828"
|
||||
|
@ -3470,6 +3475,14 @@ compressible@^2.0.0:
|
|||
dependencies:
|
||||
mime-db ">= 1.43.0 < 2"
|
||||
|
||||
compressorjs@^1.0.7:
|
||||
version "1.0.7"
|
||||
resolved "https://registry.yarnpkg.com/compressorjs/-/compressorjs-1.0.7.tgz#67cd0b3b9ac97540515b22b334dc32eb92b730b1"
|
||||
integrity sha512-ca+H8CGrn0LG103//VQmXBbNdvzvHiW26LGdWncp4RmLNbNQjaaFWIUxMN9++hbhGobLtofkHoxzzXGisNyD3w==
|
||||
dependencies:
|
||||
blueimp-canvas-to-blob "^3.28.0"
|
||||
is-blob "^2.1.0"
|
||||
|
||||
compute-scroll-into-view@^1.0.16:
|
||||
version "1.0.16"
|
||||
resolved "https://registry.yarnpkg.com/compute-scroll-into-view/-/compute-scroll-into-view-1.0.16.tgz#5b7bf4f7127ea2c19b750353d7ce6776a90ee088"
|
||||
|
@ -6275,6 +6288,11 @@ is-binary-path@~2.1.0:
|
|||
dependencies:
|
||||
binary-extensions "^2.0.0"
|
||||
|
||||
is-blob@^2.1.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/is-blob/-/is-blob-2.1.0.tgz#e36cd82c90653f1e1b930f11baf9c64216a05385"
|
||||
integrity sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==
|
||||
|
||||
is-buffer@^1.1.5, is-buffer@~1.1.6:
|
||||
version "1.1.6"
|
||||
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
|
||||
|
|
Reference in New Issue