Remove mosaic TypeScript size implementation

Will assist in solving #48 faster
This commit is contained in:
dangered wolf 2022-08-28 15:23:15 -04:00
parent 0f938af10f
commit e95ec915f2
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
4 changed files with 13 additions and 92 deletions

View file

@ -28,94 +28,14 @@ export const handleMosaic = async (
}
}
const size = calcSize(
mediaList.map(i => ({ width: i.width, height: i.height } as Size))
);
return {
height: size.height,
width: size.width,
WARNING: 'height and width of mosaic photos are deprecated and will be removed.',
height: 0,
width: 0,
formats: {
jpeg: `${baseUrl}jpeg/${id}${path}`,
webp: `${baseUrl}webp/${id}${path}`
}
} as APIMosaicPhoto;
} as unknown as APIMosaicPhoto;
}
};
/* TypeScript Port of https://github.com/FixTweet/mosaic/blob/feature/size-endpoint/src/mosaic.rs#L236
We use this to generate accurate mosaic sizes which helps Discord render it correctly */
const SPACING_SIZE = 10;
/* This number will be multiplied by the height and weight
if all combined images are over 2000 pixels in height or width.
In my tests setting this to 0.5 increased performance by 4x (at the cost of 50% resolution)
NOTE: This only works for when there's 4 images supplied. */
const BIG_IMAGE_MULTIPLIER = 1;
const calcSize = (images: Size[]): Size => {
if (images.length === 1) {
return images[0];
} else if (images.length === 2) {
const size = calcHorizontalSize(images[0], images[1]);
return size as Size;
} else if (images.length === 3) {
const size = calcHorizontalSize(images[0], images[1]);
const thirdSize = calcVerticalSize(size, images[2]);
if (thirdSize.secondHeight * 1.5 > size.height) {
return calcHorizontalSize(size, images[2]) as Size;
} else {
return thirdSize as Size;
}
} else if (images.length === 4) {
const top = calcHorizontalSize(images[0], images[1]);
const bottom = calcHorizontalSize(images[2], images[3]);
const all = calcVerticalSize(top, bottom);
const sizeMult = all.width > 2000 || all.height > 2000 ? BIG_IMAGE_MULTIPLIER : 1;
return {
width: all.width * sizeMult,
height: all.height * sizeMult
};
} else {
throw new Error('Invalid number of images');
}
};
const calcHorizontalSize = (first: Size, second: Size): HorizontalSize => {
let small = second;
let big = first;
let swapped = false;
if (second.height > first.height) {
small = first;
big = second;
swapped = true;
}
const smallWidth = Math.round((big.height / small.height) * small.width);
return {
width: smallWidth + SPACING_SIZE + big.width,
height: big.height,
firstWidth: swapped ? smallWidth : big.width,
secondWidth: swapped ? big.width : smallWidth
} as HorizontalSize;
};
const calcVerticalSize = (first: Size, second: Size): VerticalSize => {
let small = second;
let big = first;
let swapped = false;
if (second.width > first.width) {
small = first;
big = second;
swapped = true;
}
const smallHeight = Math.round((big.width / small.width) * small.height);
return {
width: big.width,
height: smallHeight + SPACING_SIZE + big.height,
firstHeight: swapped ? smallHeight : big.height,
secondHeight: swapped ? big.height : smallHeight
} as VerticalSize;
};

View file

@ -220,12 +220,17 @@ export const handleStatus = async (
/* Push the raw photo-related headers */
headers.push(
`<meta name="twitter:image" content="${photo.url}"/>`,
`<meta name="twitter:image:width" content="${photo.width}"/>`,
`<meta name="twitter:image:height" content="${photo.height}"/>`,
`<meta name="og:image" content="${photo.url}"/>`,
`<meta name="og:image:width" content="${photo.width}"/>`,
`<meta name="og:image:height" content="${photo.height}"/>`
);
if (!tweet.media.mosaic) {
headers.push(
`<meta name="twitter:image:width" content="${photo.width}"/>`,
`<meta name="twitter:image:height" content="${photo.height}"/>`,
`<meta name="og:image:width" content="${photo.width}"/>`,
`<meta name="og:image:height" content="${photo.height}"/>`
);
}
}
/* We have external media available to us (i.e. YouTube videos) */

View file

@ -88,8 +88,6 @@ interface APIPhoto {
interface APIMosaicPhoto {
type: 'mosaic_photo';
width: number;
height: number;
formats: {
webp: string;
jpeg: string;

View file

@ -171,8 +171,6 @@ test('API fetch multi-photo Tweet', async () => {
expect(photos[3].height).toEqual(418);
expect(tweet.media?.mosaic).toBeTruthy();
const mosaic = tweet.media?.mosaic as APIMosaicPhoto;
expect(mosaic.width).toEqual(1610);
expect(mosaic.height).toEqual(846);
expect(mosaic.formats?.jpeg).toEqual(
'https://mosaic.fxtwitter.com/jpeg/1554870933449482240/FZQCeMmXwAAOJTt/FZQCl-lWIAMtoW9/FZQCsQPX0AIbY6H/FZQCxmLXEAMST4q'
);