Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculate the PPI of an image

PPI Calculation

Variables

  • ppi as int
  • pageResolution in px
  • imageResolution in px
  • pageSize in inches

Sample values

{
	ppi: 96,
	pageResolution:  { longEdge: 1548, shortEdge: 741 },
	imageResolution: { longEdge: 1506, shortEdge: 699 },
	pageSize:        { longEdge: 16.125984, shortEdge: 7.716535 },
}

Formula

  • Get percentage size of image longEdge relative to page
  • Use percentage to to get image size in inches
  • Multiply inches by ppi

Calculation

// get image's percentage of page
imageSizePercentage = imageResolution.longEdge / pageResolution.longEdge // e.g. 97(%)

// calculate physical size of image in inches
physicalSize = ((pageSize.longEdge * imageSizePercentage / ppi) * ppi) // e.g. 15.64220448(in)

// final output size in px
outputSize = physicalSize * ppi // e.g. 4692(px)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.