ปรับขนาดไฟล์

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2025-04-02 10:54:13 +07:00
parent a9f1bbd1dc
commit 6f07e15a78

View file

@ -262,13 +262,47 @@ async function capturePhoto() {
)
if (!imageBlob) return
const fileName = 'photo.png'
// Canvas
const imgElement = new Image()
imgElement.src = URL.createObjectURL(imageBlob)
await imgElement.decode()
// 1024px
const maxSize = 1024
let { width, height } = imgElement
if (width > maxSize || height > maxSize) {
if (width > height) {
height *= maxSize / width
width = maxSize
} else {
width *= maxSize / height
height = maxSize
}
}
// Canvas
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
if (ctx) {
ctx.drawImage(imgElement, 0, 0, width, height)
}
// Canvas Blob
const resizedBlob: Blob = await new Promise((resolve) =>
canvas.toBlob((blob) => resolve(blob!), 'image/png', 0.8)
)
//
const file = new File([imageBlob], fileName, { type: 'image/png' })
const file = new File([resizedBlob], fileName, { type: 'image/png' })
fileImg.value = file
//
await camera.value?.stop()
const url = URL.createObjectURL(imageBlob)
const url = URL.createObjectURL(resizedBlob)
img.value = url
console.log(file)
}
/** function เปลี่ยนรูปภาพ*/