ปรับขนาดไฟล์
This commit is contained in:
parent
a9f1bbd1dc
commit
6f07e15a78
1 changed files with 36 additions and 2 deletions
|
|
@ -262,13 +262,47 @@ async function capturePhoto() {
|
||||||
)
|
)
|
||||||
if (!imageBlob) return
|
if (!imageBlob) return
|
||||||
const fileName = 'photo.png'
|
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
|
fileImg.value = file
|
||||||
//แสดงรูป
|
//แสดงรูป
|
||||||
await camera.value?.stop()
|
await camera.value?.stop()
|
||||||
const url = URL.createObjectURL(imageBlob)
|
const url = URL.createObjectURL(resizedBlob)
|
||||||
img.value = url
|
img.value = url
|
||||||
|
console.log(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** function เปลี่ยนรูปภาพ*/
|
/** function เปลี่ยนรูปภาพ*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue