Files
cube/apps/piano-sheet/frontend/src/lib/compress.js
T
Fam Zheng 58f344db85
deploy piano-sheet / build-and-deploy (push) Successful in 1m17s
piano-sheet(upload): mobile/pad first,主入口直调后置摄像头
- 主 CTA「拍下一页」+ capture=environment,副 CTA「从相册」multiple
- 底部 sticky 上传按钮,iOS safe area 兜住刘海/Home indicator
- 客户端压缩 1800px JPEG 0.85(createImageBitmap + EXIF 自动旋转)
- 页码 ↑↓ 移动 + ✕ 删除替代旧拖拽
2026-05-05 10:57:38 +01:00

33 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 移动端拍照原图常 512MB15001800px 长边 + JPEG 0.85 已经够看清楚音符。
// 用 createImageBitmap 的 imageOrientation: 'from-image' 自动按 EXIF 旋转,
// 否则横拍照在 canvas 里会变成竖图。
export async function compressImage(file, { maxEdge = 1800, quality = 0.85 } = {}) {
if (!file || !file.type || !file.type.startsWith('image/')) return file
let bitmap
try {
bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' })
} catch {
return file
}
const { width, height } = bitmap
const scale = Math.min(1, maxEdge / Math.max(width, height))
const w = Math.max(1, Math.round(width * scale))
const h = Math.max(1, Math.round(height * scale))
const canvas = document.createElement('canvas')
canvas.width = w
canvas.height = h
const ctx = canvas.getContext('2d')
ctx.drawImage(bitmap, 0, 0, w, h)
bitmap.close?.()
const blob = await new Promise((res) => canvas.toBlob(res, 'image/jpeg', quality))
if (!blob || blob.size >= file.size) return file
const baseName = file.name?.replace(/\.[^.]+$/, '') || 'photo'
return new File([blob], `${baseName}.jpg`, { type: 'image/jpeg', lastModified: Date.now() })
}