Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/http/schemas/transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ export const transformationOptionsSchema = {
resize: { type: 'string', enum: ['cover', 'contain', 'fill'] },
format: { type: 'string', enum: ['origin', 'avif'] },
quality: { type: 'integer', minimum: 20, maximum: 100 },
gravity: {
type: 'string',
enum: ['no', 'so', 'ea', 'we', 'noea', 'nowe', 'soea', 'sowe', 'ce', 'sm', 'fp'],
},
x_offset: { type: 'number', examples: [0.1, 100] },
y_offset: { type: 'number', examples: [0.1, -100] },
} as const
46 changes: 46 additions & 0 deletions src/storage/renderer/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export interface TransformOptions {
resize?: 'cover' | 'contain' | 'fill'
format?: 'origin' | 'avif'
quality?: number
gravity?: 'no' | 'so' | 'ea' | 'we' | 'noea' | 'nowe' | 'soea' | 'sowe' | 'ce' | 'sm' | 'fp'
x_offset?: number
y_offset?: number
}

const {
Expand Down Expand Up @@ -115,6 +118,40 @@ export class ImageRenderer extends Renderer {
segments.push(`format:${options.format}`)
}

if (options.gravity) {
switch (options.gravity) {
case 'sm': {
segments.push(`gravity:${options.gravity}`)
break
}
case 'fp': {
if (
!(
options.x_offset &&
options.y_offset &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 0 a valid value for this? This would throw if x or y is 0 because 0 is "falsey" ... maybe replace it with isNaN

options.x_offset <= 1 &&
options.y_offset <= 1 &&
options.x_offset >= 0 &&
options.y_offset >= 0
)
) {
throw new StorageBackendError(
'Invalid focus point',
400,
'Focal point requires x and y coordinates within 0-1 range'
)
}
segments.push(`gravity:${options.gravity}:${options.x_offset}:${options.y_offset}`)
break
}
default: {
segments.push(
`gravity:${options.gravity}:${options.x_offset ?? 0}:${options.y_offset ?? 0}`
)
}
}
}

return segments
}

Expand Down Expand Up @@ -170,6 +207,15 @@ export class ImageRenderer extends Renderer {
case 'quality':
all.quality = parseInt(value, 10)
break
case 'gravity':
all.gravity = value
break
case 'x_offset':
all.x_offset = parseFloat(value)
break
case 'y_offset':
all.y_offset = parseFloat(value)
break
}
return all
}, {} as TransformOptions)
Expand Down