我通过使用base64发送/接收我的图像。 我有一个base64字符串,我想将其压缩到我的大小。
例如,我想将照片大小减小到100kb。
有可能吗?
答案 0 :(得分:1)
这是一个有趣的挑战,因为它涉及二进制搜索,直到找到合适的大小。我不建议您使用base64而不是blob解决此问题,因为您应该将其真正处理为二进制(blob),否则它将占用base64约3倍的数据
此代码包括调整大小,您可以设置最大宽度/高度,并且仍然能够保持宽高比和自动质量查找,直到找到与MAX_SIZE相匹配的正确质量为止
console.log('Downloading lorem ipsum image to simulate a file from user input')
fetch('https://picsum.photos/1920/1080/?random')
.then(res => res.blob())
.then(blob => {
const img = new Image()
img.src = URL.createObjectURL(blob)
console.log(`Original image size (at 1920x1080) is: ${blob.size} bytes`)
console.log('URL to original image:', img.src)
img.onload = () => resize(img, 'jpeg').then(blob => {
console.log('Final blob size', blob.size)
console.log('Final blob url:', URL.createObjectURL(blob))
console.log('\nNow with webp\n')
resize(img, 'webp').then(blob => {
console.log('Final blob size', blob.size)
console.log('Final blob url:', URL.createObjectURL(blob))
})
})
})
const MAX_WIDTH = 1280
const MAX_HEIGHT = 720
const MAX_SIZE = 100000 // 100kb
async function resize(img, type = 'jpeg') {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0)
let width = img.width
let height = img.height
let start = 0
let end = 1
let last, accepted, blob
// keep portration
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width
width = MAX_WIDTH
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height
height = MAX_HEIGHT
}
}
canvas.width = width
canvas.height = height
console.log('Scaling image down to max 1280x720 while keeping aspect ratio')
ctx.drawImage(img, 0, 0, width, height)
accepted = blob = await new Promise(rs => canvas.toBlob(rs, 'image/'+type, 1))
if (blob.size < MAX_SIZE) {
console.log('No quality change needed')
return blob
} else {
console.log(`Image size after scaling ${blob.size} bytes`)
console.log('Image sample after resizeing with losseless compression:', URL.createObjectURL(blob))
}
// Binary search for the right size
while (true) {
const mid = Math.round( ((start + end) / 2) * 100 ) / 100
if (mid === last) break
last = mid
blob = await new Promise(rs => canvas.toBlob(rs, 'image/'+type, mid))
console.log(`Quality set to ${mid} gave a Blob size of ${blob.size} bytes`)
if (blob.size > MAX_SIZE) { end = mid }
if (blob.size < MAX_SIZE) { start = mid; accepted = blob }
}
return accepted
}