crop avatars to what iOS expects

pull/1512/head
Audric Ackermann 4 years ago
parent 694be0cd55
commit 8c4e071c00
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -465,9 +465,10 @@
file: new Blob([data.data], { file: new Blob([data.data], {
type: avatar.contentType, type: avatar.contentType,
}), }),
maxMeasurements: { },
maxSize: 1000 * 1024, // 1Mb for our profile picture {
}, maxSide: 640,
maxSize: 1000 * 1024,
} }
); );
const dataResized = await window.Signal.Types.Attachment.arrayBufferFromFile( const dataResized = await window.Signal.Types.Attachment.arrayBufferFromFile(

@ -23,10 +23,27 @@ export async function updateOpenGroup(
fileReader.readAsArrayBuffer(attachment.file); fileReader.readAsArrayBuffer(attachment.file);
}); });
const avatarAttachment: any = await readFile({ file: avatar }); const avatarAttachment: any = await readFile({ file: avatar });
// We want a square for iOS
const withBlob = await window.Signal.Util.AttachmentUtil.autoScale(
{
contentType: avatar.type,
file: new Blob([avatarAttachment.data], {
type: avatar.contentType,
}),
},
{
maxSide: 640,
maxSize: 1000 * 1024,
}
);
const dataResized = await window.Signal.Types.Attachment.arrayBufferFromFile(
withBlob.file
);
// const tempUrl = window.URL.createObjectURL(avatar); // const tempUrl = window.URL.createObjectURL(avatar);
// Get file onto public chat server // Get file onto public chat server
const fileObj = await API.serverAPI.putAttachment(avatarAttachment.data); const fileObj = await API.serverAPI.putAttachment(dataResized);
if (fileObj === null) { if (fileObj === null) {
// problem // problem
window.log.warn('File upload failed'); window.log.warn('File upload failed');

@ -6,8 +6,15 @@ export interface MaxScaleSize {
maxSize?: number; maxSize?: number;
maxHeight?: number; maxHeight?: number;
maxWidth?: number; maxWidth?: number;
maxSide?: number; // use this to make avatars cropped if too big and centered if too small.
} }
/**
* Scale down an image to fit in the required dimension.
* Note: This method won't crop if needed,
* @param attachment The attachment to scale down
* @param maxMeasurements any of those will be used if set
*/
export async function autoScale<T extends { contentType: string; file: any }>( export async function autoScale<T extends { contentType: string; file: any }>(
attachment: T, attachment: T,
maxMeasurements?: MaxScaleSize maxMeasurements?: MaxScaleSize
@ -22,19 +29,31 @@ export async function autoScale<T extends { contentType: string; file: any }>(
const url = URL.createObjectURL(file); const url = URL.createObjectURL(file);
const img = document.createElement('img'); const img = document.createElement('img');
img.onerror = reject; img.onerror = reject;
// tslint:disable-next-line: cyclomatic-complexity
img.onload = () => { img.onload = () => {
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
if (
maxMeasurements?.maxSide &&
(maxMeasurements?.maxHeight || maxMeasurements?.maxWidth)
) {
reject('Cannot have maxSide and another dimension set together');
}
const maxSize = const maxSize =
maxMeasurements?.maxSize || maxMeasurements?.maxSize ||
Constants.CONVERSATION.MAX_ATTACHMENT_FILESIZE_BYTES; Constants.CONVERSATION.MAX_ATTACHMENT_FILESIZE_BYTES;
const maxHeight = maxMeasurements?.maxHeight || 4096; const makeSquare = Boolean(maxMeasurements?.maxSide);
const maxWidth = maxMeasurements?.maxWidth || 4096; const maxHeight =
maxMeasurements?.maxHeight || maxMeasurements?.maxSide || 4096;
const maxWidth =
maxMeasurements?.maxWidth || maxMeasurements?.maxSide || 4096;
if ( if (
img.naturalWidth <= maxWidth && img.naturalWidth <= maxWidth &&
img.naturalHeight <= maxHeight && img.naturalHeight <= maxHeight &&
file.size <= maxSize file.size <= maxSize &&
!makeSquare
) { ) {
resolve(attachment); resolve(attachment);
return; return;
@ -55,8 +74,9 @@ export async function autoScale<T extends { contentType: string; file: any }>(
const canvas = (loadImage as any).scale(img, { const canvas = (loadImage as any).scale(img, {
canvas: true, canvas: true,
maxWidth, maxWidth: makeSquare ? maxMeasurements?.maxSide : maxWidth,
maxHeight, maxHeight: makeSquare ? maxMeasurements?.maxSide : maxHeight,
crop: makeSquare,
}); });
let quality = 0.95; let quality = 0.95;
let i = 4; let i = 4;

Loading…
Cancel
Save