使用image-to-base64把在线的图片链接转换成base64,可用于claude3识图
const imageToBase64 = require('image-to-base64')
const handleImageToBase64 = async ({ visionImgList }) => {
const order = async ({ visionImgList }) => {
//item.url 示例:'https://static.xutongbao.top/ai/wx/290289803149-3731757233684041524.jpg'
const promises = visionImgList.map(async (item) => {
return await imageToBase64(item.url).catch((error) => {
console.log(error) // Logs an error if there was one
})
})
let visionImgListTemp = []
for (let i = 0; i < promises.length; i++) {
let base64Str = await promises[i]
let imgUrl = visionImgList[i].url.toLowerCase()
visionImgList[i].url.toLowerCase().includes('jpg')
let media_type = ''
if (imgUrl.includes('.jpg')) {
media_type = 'image/jpeg'
} else if (imgUrl.includes('.jpeg')) {
media_type = 'image/jpeg'
} else if (imgUrl.includes('.png')) {
media_type = 'image/png'
} else if (imgUrl.includes('.webp')) {
media_type = 'image/webp'
}
visionImgListTemp.push({
type: 'image',
source: {
type: 'base64',
media_type, //'image/jpeg', 'image/png', 'image/gif' or 'image/webp'
data: base64Str,
},
})
}
return visionImgListTemp
}
let visionImgListTemp = await order({ visionImgList })
return visionImgListTemp
}
最简demo
const imageToBase64 = require('image-to-base64')
if (type === 'image-to-base64') {
let response = await imageToBase64(
'https://static.xutongbao.top/ai/wx/290289803149-3731757233684041524.jpg'
).catch((error) => {
console.log(error) // Logs an error if there was one
})
res.send({
code: 200,
data: {
response,
},
message: '成功',
})
}
我开发chatgpt网站: