Skip to content

Commit

Permalink
fix: readd metadata annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 16, 2021
1 parent f46f2c5 commit afa0b4b
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 33 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/directives/blur.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface BlurOptions {
blur: string
Expand All @@ -14,6 +15,8 @@ export const blur: Directive<BlurOptions> = (config, ctx) => {
if (!blur) return

return function blurTransform(image) {
setMetadata(image, 'blur', blur)

return image.blur(blur)
}
}
9 changes: 5 additions & 4 deletions packages/core/src/directives/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Directive } from "../types";
import { background as getBackground } from './background'
import { setMetadata } from "../lib/metadata";
import { getBackground } from './background'

export interface FlattenOptions {
flatten: '' | 'true'
Expand All @@ -8,11 +9,11 @@ export interface FlattenOptions {
export const flatten: Directive<FlattenOptions> = (config, ctx) => {
if (config.flatten !== '' && config.flatten !== 'true') return

const background = getBackground(config, ctx)

return function flattenTransform(image) {
setMetadata(image, 'flatten', true)

return image.flatten({
background
background: getBackground(config, image)
})
}
}
3 changes: 3 additions & 0 deletions packages/core/src/directives/flip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface FlipOptions {
flip: '' | 'true'
Expand All @@ -8,6 +9,8 @@ export const flip: Directive<FlipOptions> = ({ flip }, ctx) => {
if (flip !== '' && flip !== 'true') return

return function flipTransform(image) {
setMetadata(image, 'flip', true)

return image.flip()
}
}
5 changes: 4 additions & 1 deletion packages/core/src/directives/flop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface FlopOptions {
flop: '' | 'true'
Expand All @@ -7,7 +8,9 @@ export interface FlopOptions {
export const flop: Directive<FlopOptions> = ({ flop }, ctx) => {
if (flop !== '' && flop !== 'true') return

return function flipTransform(image) {
return function flopTransform(image) {
setMetadata(image, 'flop', true)

return image.flop()
}
}
12 changes: 9 additions & 3 deletions packages/core/src/directives/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Directive } from "../types";
import { quality as getQuality } from './quality'
import { setMetadata } from "../lib/metadata";
import { getQuality } from './quality'
import { getProgressive } from './progressive'

export const formatValues = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'heic', 'webp', 'tiff'] as const

Expand All @@ -19,10 +21,14 @@ export const format: Directive<FormatOptions> = (config, ctx) => {
}
if (!format) return

const quality = getQuality(config, ctx)

return function formatTransform(image) {
setMetadata(image, 'format', format)

//@ts-ignore
return image.toFormat(format, { quality })
return image.toFormat(format, {
quality: getQuality(config, image),
progressive: getProgressive(config, image)
})
}
}
3 changes: 3 additions & 0 deletions packages/core/src/directives/grayscale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface GrayscaleOptions {
grayscale: '' | 'true'
Expand All @@ -8,6 +9,8 @@ export const grayscale: Directive<GrayscaleOptions> = ({ grayscale }, ctx) => {
if (grayscale !== '' && grayscale !== 'true') return

return function grayscaleTransform(image) {
setMetadata(image, 'grayscale', true)

return image.grayscale()
}
}
7 changes: 6 additions & 1 deletion packages/core/src/directives/hsb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface HSBOptions {
hue: string
Expand All @@ -12,9 +13,13 @@ export const hsb: Directive<HSBOptions> = (config, ctx) => {
const saturation = config.saturation && parseFloat(config.saturation)
const brightness = config.brightness && parseFloat(config.brightness)

if(!hue && !saturation && !brightness) return
if (!hue && !saturation && !brightness) return

return function hsbTransform(image) {
setMetadata(image, 'hue', hue)
setMetadata(image, 'saturation', saturation)
setMetadata(image, 'brightness', brightness)

return image.modulate({
hue: hue || 0,
saturation: saturation || 1,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/directives/invert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface InvertOptions {
invert: '' | 'true'
Expand All @@ -8,6 +9,8 @@ export const invert: Directive<InvertOptions> = ({ invert }, ctx) => {
if (invert !== '' && invert !== 'true') return

return function invertTransform(image) {
setMetadata(image, 'invert', true)

return image.negate()
}
}
3 changes: 3 additions & 0 deletions packages/core/src/directives/median.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface MedianOptions {
median: string
Expand All @@ -10,6 +11,8 @@ export const median: Directive<MedianOptions> = (config, ctx) => {
if (!median) return

return function medianTransform(image) {
setMetadata(image, 'median', median)

return image.median(median)
}
}
5 changes: 4 additions & 1 deletion packages/core/src/directives/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface NormalizeOptions {
normalize: '' | 'true'
}

export const normalize: Directive<NormalizeOptions> = ({ normalize }, ctx) => {
if(normalize !== '' && normalize !== 'true') return
if (normalize !== '' && normalize !== 'true') return

return function normalizeTransform(image) {
setMetadata(image, 'normalize', true)

return image.normalize()
}
}
57 changes: 37 additions & 20 deletions packages/core/src/directives/resize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Directive } from "../types";
import { fit as getFit } from './fit'
import { position as getPosition } from './position'
import { kernel as getKernel } from './kernel'
import { background as getBackground } from './background'
import { getFit } from './fit'
import { getPosition } from './position'
import { getKernel } from './kernel'
import { getBackground } from './background'
import { setMetadata, getMetadata } from "../lib/metadata";

export interface ResizeOptions {
width: string
Expand All @@ -13,26 +14,42 @@ export interface ResizeOptions {

export const resize: Directive<ResizeOptions> = (config, ctx) => {

const width = config.width ? parseInt(config.width) : undefined
const w = config.w ? parseInt(config.w) : undefined
const height = config.height ? parseInt(config.height) : undefined
const h = config.h ? parseInt(config.h) : undefined
const width = config.width
? parseInt(config.width)
: config.w
? parseInt(config.w)
: undefined
const height = config.height
? parseInt(config.height)
: config.h
? parseInt(config.h)
: undefined

if (!width && !w && !height && !h) return

const fit = getFit(config, ctx)
const position = getPosition(config, ctx)
const kernel = getKernel(config, ctx)
const background = getBackground(config, ctx)
if (!width && !height) return

return function resizeTransform(image) {

if (!height) {
const w = getMetadata(image, 'width')
const h = getMetadata(image, 'height')
setMetadata(image, 'height', Math.round((width! / w) * h))
setMetadata(image, 'width', width)
}

if (!width) {
const w = getMetadata(image, 'width')
const h = getMetadata(image, 'height')
setMetadata(image, 'width', Math.round((height! / h) * w))
setMetadata(image, 'height', height)
}

return image.resize({
width: width || w,
height: height || h,
fit,
position,
kernel,
background
width: width,
height: height,
fit: getFit(config, image),
position: getPosition(config, image),
kernel: getKernel(config, image),
background: getBackground(config, image)
})
}
}
10 changes: 7 additions & 3 deletions packages/core/src/directives/rotate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Directive } from "../types";
import { background as getBackground } from './background'
import { setMetadata } from "../lib/metadata";
import { getBackground } from './background'

export interface RotateOptions {
rotate: string
Expand All @@ -10,9 +11,12 @@ export const rotate: Directive<RotateOptions> = (config, ctx) => {

if (!rotate) return

const background = getBackground(config, ctx)

return function rotateTransform(image) {
return image.rotate(rotate, { background })
setMetadata(image, 'rotate', rotate)

return image.rotate(rotate, {
background: getBackground(config, image)
})
}
}
3 changes: 3 additions & 0 deletions packages/core/src/directives/tint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive } from "../types";
import { setMetadata } from "../lib/metadata";

export interface TintOptions {
tint: string
Expand All @@ -8,6 +9,8 @@ export const tint: Directive<TintOptions> = ({ tint }, ctx) => {
if (typeof tint !== 'string' || !tint.length) return

return function tintTransform(image) {
setMetadata(image, 'tint', '#' + tint)

return image.tint('#' + tint)
}
}

0 comments on commit afa0b4b

Please sign in to comment.