Skip to content

Instantly share code, notes, and snippets.

@nerdyman
Created November 9, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdyman/409db199d31edcce80006b3ba60b9942 to your computer and use it in GitHub Desktop.
Save nerdyman/409db199d31edcce80006b3ba60b9942 to your computer and use it in GitHub Desktop.
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