File size: 3,801 Bytes
238d8e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/** Crop region in natural image pixels. */
export interface CropRect {
  x: number
  y: number
  width: number
  height: number
}

export interface DisplayLayout {
  offsetX: number
  offsetY: number
  width: number
  height: number
}

const MIN_CROP_PX = 48

export function clampCropRect(
  crop: CropRect,
  naturalW: number,
  naturalH: number,
): CropRect {
  const minW = Math.min(MIN_CROP_PX, naturalW)
  const minH = Math.min(MIN_CROP_PX, naturalH)
  let { x, y, width, height } = crop

  width = Math.max(minW, Math.min(width, naturalW))
  height = Math.max(minH, Math.min(height, naturalH))
  x = Math.max(0, Math.min(x, naturalW - width))
  y = Math.max(0, Math.min(y, naturalH - height))

  return { x, y, width, height }
}

export function defaultCropRect(naturalW: number, naturalH: number): CropRect {
  const marginX = naturalW * 0.08
  const marginY = naturalH * 0.08
  return clampCropRect(
    {
      x: marginX,
      y: marginY,
      width: naturalW - marginX * 2,
      height: naturalH - marginY * 2,
    },
    naturalW,
    naturalH,
  )
}

export function fullCropRect(naturalW: number, naturalH: number): CropRect {
  return { x: 0, y: 0, width: naturalW, height: naturalH }
}

/** object-contain layout of an image inside a container. */
export function computeDisplayLayout(
  containerW: number,
  containerH: number,
  naturalW: number,
  naturalH: number,
): DisplayLayout {
  if (containerW <= 0 || containerH <= 0 || naturalW <= 0 || naturalH <= 0) {
    return { offsetX: 0, offsetY: 0, width: 0, height: 0 }
  }

  const scale = Math.min(containerW / naturalW, containerH / naturalH)
  const width = naturalW * scale
  const height = naturalH * scale
  return {
    offsetX: (containerW - width) / 2,
    offsetY: (containerH - height) / 2,
    width,
    height,
  }
}

export function cropToDisplay(
  crop: CropRect,
  layout: DisplayLayout,
  naturalW: number,
  naturalH: number,
) {
  const sx = layout.width / naturalW
  const sy = layout.height / naturalH
  return {
    x: layout.offsetX + crop.x * sx,
    y: layout.offsetY + crop.y * sy,
    width: crop.width * sx,
    height: crop.height * sy,
  }
}

export function displayToCrop(
  display: { x: number; y: number; width: number; height: number },
  layout: DisplayLayout,
  naturalW: number,
  naturalH: number,
): CropRect {
  const sx = naturalW / layout.width
  const sy = naturalH / layout.height
  return clampCropRect(
    {
      x: (display.x - layout.offsetX) * sx,
      y: (display.y - layout.offsetY) * sy,
      width: display.width * sx,
      height: display.height * sy,
    },
    naturalW,
    naturalH,
  )
}

export async function applyCrop(
  source: HTMLImageElement,
  crop: CropRect,
): Promise<{ image: HTMLImageElement; file: File; url: string }> {
  const rect = clampCropRect(crop, source.naturalWidth, source.naturalHeight)
  const canvas = document.createElement('canvas')
  canvas.width = Math.round(rect.width)
  canvas.height = Math.round(rect.height)
  const ctx = canvas.getContext('2d')
  if (!ctx) throw new Error('Canvas not supported')

  ctx.drawImage(
    source,
    rect.x,
    rect.y,
    rect.width,
    rect.height,
    0,
    0,
    canvas.width,
    canvas.height,
  )

  const blob = await new Promise<Blob>((resolve, reject) => {
    canvas.toBlob(
      (b) => (b ? resolve(b) : reject(new Error('Failed to encode crop'))),
      'image/jpeg',
      0.92,
    )
  })

  const file = new File([blob], 'crop.jpg', { type: 'image/jpeg' })
  const url = URL.createObjectURL(blob)
  const image = await new Promise<HTMLImageElement>((resolve, reject) => {
    const img = new Image()
    img.onload = () => resolve(img)
    img.onerror = () => reject(new Error('Failed to load cropped image'))
    img.src = url
  })

  return { image, file, url }
}