Berikut adalah contoh method GET untuk menggunakan API key di api.imgbb.com/v1/upload:

import fetch from 'node-fetch';
import { FormData, Blob } from 'formdata-node';
import { fileTypeFromBuffer } from 'file-type'

/**
 * Upload image to imgbb.com
 * Supported mimetype:
 * - `image/jpeg`
 * - `image/jpg`
 * - `image/png`
 * @param {Buffer} buffer Image Buffer
 * @param {string} apiKey API Key for imgbb.com
 * @return {Promise<string>}
 */
export default async (buffer, apiKey) => {
  const { ext, mime } = await fileTypeFromBuffer(buffer)
  let form = new FormData()
  const blob = new Blob([buffer.toArrayBuffer()], { type: mime })
  form.append('file', blob, 'tmp.' + ext)
  let res = await fetch('https://api.imgbb.com/v1/upload?key=' + apiKey, {
    method: 'POST',
    body: form
  })
  let img = await res.json()
  if (img.error) throw img.error
  return img.data.display_url
}

Anda dapat menggunakan method ini dengan memasukkan buffer gambar dan API key Anda. Contoh penggunaan:

const buffer = // Buffer gambar yang ingin diupload
const apiKey = 'API_KEY_ANDA'

try {
  const imageUrl = await uploadImageToImgbb(buffer, apiKey)
  console.log('Image URL:', imageUrl)
} catch (error) {
  console.error('Error uploading image:', error)
}

Pastikan Anda telah mengganti API_KEY_ANDA dengan API key Anda yang valid


原文地址: https://www.cveoy.top/t/topic/iySh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录