Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add experimental image orientation fix #724

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dev": "rollup -cw",
"build": "rollup -c",
"test": "vitest run",
"test:watch": "vitest watch",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get into watch mode without this, but I didn't mean to add it to the commit. Will drop.

"coverage": "vitest run --coverage"
},
"devDependencies": {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion packages/core/src/__tests__/apply-transforms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('applyTransforms', () => {
expect(metadata).not.toHaveProperty('iptc')
})

it('metadata stripping can be disabled', async () => {
it('metadata stripping can be disabled via boolean', async () => {
const t = vi.fn((i) => i)

const { metadata } = await applyTransforms([t], img, false)
Expand All @@ -40,6 +40,16 @@ describe('applyTransforms', () => {
expect(metadata).toHaveProperty('xmp')
})

it('metadata stripping can be disabled via options object', async () => {
const t = vi.fn((i) => i)

const { metadata } = await applyTransforms([t], img, { removeMetadata: false })

expect(t).toBeCalled()
expect(metadata).toHaveProperty('icc')
expect(metadata).toHaveProperty('xmp')
})

it('returns the image data & info', async () => {
const t = vi.fn((i) => i)

Expand Down
61 changes: 58 additions & 3 deletions packages/core/src/lib/apply-transforms.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
import type { Sharp } from 'sharp'
import type { ImageTransformation, TransformResult } from '../types.js'
import type { ApplyTransformsOptions, ImageTransformation, TransformResult } from '../types.js'
import { METADATA } from './metadata.js'
import sharp from 'sharp'

const defaultOptions: ApplyTransformsOptions = {
removeMetadata: true
}

async function applyPreTransformOrientation(
transforms: ImageTransformation[],
image: Sharp,
opts: ApplyTransformsOptions
) {
if (!opts.experimental?.preserveInitialOrientation) return image

const initialOrientation = image[METADATA].orientation
if (!initialOrientation || initialOrientation === 1) return image

let tempImage = image.clone()
tempImage[METADATA] = { ...image[METADATA] }

for (const transform of transforms) {
tempImage = await transform(tempImage)
}

const willFlip = !!tempImage[METADATA].flip
const willFlop = !!tempImage[METADATA].flop
const willRotate = !!tempImage[METADATA].rotate

image.rotate() // no args -> do the exif orientation

if (willFlip || willFlop || willRotate) {
image = sharp(await image.toBuffer())
image[METADATA] = { ...image[METADATA] }
}

return image
}

export async function applyTransforms(
transforms: ImageTransformation[],
image: Sharp,
options?: ApplyTransformsOptions
): Promise<TransformResult>
export async function applyTransforms(
transforms: ImageTransformation[],
image: Sharp,
removeMetadata = true
removeMetadata?: boolean
): Promise<TransformResult>
export async function applyTransforms(
transforms: ImageTransformation[],
image: Sharp,
optionsOrRemoveMetadata?: ApplyTransformsOptions | boolean
): Promise<TransformResult> {
const opts = {
...defaultOptions,
...(typeof optionsOrRemoveMetadata === 'boolean'
? { removeMetadata: optionsOrRemoveMetadata }
: optionsOrRemoveMetadata)
}

image[METADATA] = { ...(await image.metadata()) }

if (removeMetadata) {
if (opts.removeMetadata) {
// delete the private metadata
delete image[METADATA].exif
delete image[METADATA].iptc
Expand All @@ -20,6 +73,8 @@ export async function applyTransforms(
image.withMetadata()
}

image = await applyPreTransformOrientation(transforms, image, opts)

for (const transform of transforms) {
image = await transform(image)
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading