Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lysender/5771479df5c2ae93d088f472cce3ff64 to your computer and use it in GitHub Desktop.
Save lysender/5771479df5c2ae93d088f472cce3ff64 to your computer and use it in GitHub Desktop.
sharp - resize portrait into landscape but it rotates the image instead

I have a bunch of photos from my old iPad. I have a script that create thumbnail photos from these images.

The goal is to resize the image into 150x125 and crop if necessary to achieve the result.

const options: ResizeOptions = {
  width: 150,
  height: 125,
  fit: 'cover',
}

await sharp(inFile).resize(options).toFile(outFile);

It works fine on landscape images. For some reason, portrait thumbnails are rotated as if it is adjusted to fit the given 150x125 dimension.

Found a related issue on github:

lovell/sharp#2822

Turns out, sharp automatically rotates the image due to some EXIF metadata from my iPad photos.

I don't dig deeper EXIF stuff and jump straight to solution.

The trick is to rotate the image first based on EXIF orientation then resize.

const options: ResizeOptions = {
  width: 150,
  height: 125,
  fit: 'cover',
}

await sharp(inFile).rotate().resize(options).toFile(outFile);

Pro tip!

If you can't find the Github issue of a certain repo, try searching it on the "closed" issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment