"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) }; };