You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
788 B
JavaScript

"use strict";
module.exports = function calculateThumbnailSize({ sourceWidth, sourceHeight, targetWidth, targetHeight }) {
// FIXME: Validation
// TODO: crop method, currently only implements scale/fit (crop would probably require inverting match arms, ceil instead of floor, and providing a drawing offset)
let sourceAspectRatio = sourceWidth / sourceHeight;
let targetAspectRatio = targetWidth / targetHeight;
let resizeFactor = (targetAspectRatio > sourceAspectRatio)
// Target is wider than source, constrain by height
? targetHeight / sourceHeight
// Target is taller than or equally-ratioed to source, constrain by width
: targetWidth / sourceWidth;
return {
width: Math.floor(sourceWidth * resizeFactor),
height: Math.floor(sourceHeight * resizeFactor)
};
};