Option to send photos as high quality

This commit is contained in:
Josh Perez 2021-06-25 12:08:16 -04:00 committed by GitHub
parent 6c56d5a5f1
commit 01eabf9ec6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 1263 additions and 363 deletions

View file

@ -0,0 +1,31 @@
// Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import loadImage, { LoadImageOptions } from 'blueimp-load-image';
import { IMAGE_JPEG } from '../types/MIME';
import { canvasToBlob } from './canvasToBlob';
const DEFAULT_JPEG_QUALITY = 0.85;
export async function autoOrientImage(blob: Blob): Promise<Blob> {
const options: LoadImageOptions = {
canvas: true,
orientation: true,
};
try {
const data = await loadImage(blob, options);
const { image } = data;
if (image instanceof HTMLCanvasElement) {
// We `return await`, instead of just `return`, so we capture the rejection in this
// try/catch block. See [this blog post][0] for more background.
// [0]: https://jakearchibald.com/2017/await-vs-return-vs-return-await/
return await canvasToBlob(image, IMAGE_JPEG, DEFAULT_JPEG_QUALITY);
}
throw new Error('image not a canvas');
} catch (err) {
const error = new Error('autoOrientImage: Failed to process image');
error.originalError = err;
throw error;
}
}