Axios: get μš”μ²­μ—μ„œ base64둜 이미지λ₯Ό λ³€ν™˜ν•˜λŠ” 방법

에 λ§Œλ“  2016λ…„ 11μ›” 01일  Β·  10μ½”λ©˜νŠΈ  Β·  좜처: axios/axios

이 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆλŠ” npm의 λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μžˆμŠ΅λ‹ˆκΉŒ?

axios.get('http://www.freeiconspng.com/uploads/profile-icon-9.png', {
   transformRequest: [function (data) {
       return data;
   }],
  transformResponse: [function (data) {
    return data;
  }],
});

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚˜μ™€ 같은 λ…Έλ“œμ— 있고 v >= 6 을 μ‚¬μš©ν•˜λŠ” 경우 λ³€κ²½ 사항이 μžˆμœΌλ―€λ‘œ Buffer.from λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

function getBase64(url) {
  return axios
    .get(url, {
      responseType: 'arraybuffer'
    })
    .then(response => Buffer.from(response.data, 'binary').toString('base64'))
}

μ°Έμ‘° μ•ˆμ „ 버퍼
new Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead

λͺ¨λ“  10 λŒ“κΈ€

이것은 StackOverflow에 λŒ€ν•œ 질문이라고 μƒκ°ν•©λ‹ˆλ‹€.

μ†”λ£¨μ…˜μ„ κ²€μƒ‰ν•˜λŠ” μ‚¬λžŒμ„ μœ„ν•΄ λ‹€μŒ μ½”λ“œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

get('/getImg', {
    responseType: 'arraybuffer' 
})
.then(function(result) {
    console.log(_imageEncode(result.data))
})
function _imageEncode (arrayBuffer) {
    let u8 = new Uint8Array(arrayBuffer)
    let b64encoded = btoa([].reduce.call(new Uint8Array(arrayBuffer),function(p,c){return p+String.fromCharCode(c)},''))
    let mimetype="image/jpeg"
    return "data:"+mimetype+";base64,"+b64encoded
}

이것은 우리의 μ†”λ£¨μ…˜μ΄μ—ˆκ³  맀λ ₯처럼 μž‘λ™ν•©λ‹ˆλ‹€.

return axios.get('http://example.com/image.png', { responseType: 'arraybuffer' })
      .then((response) => {
        let image = btoa(
          new Uint8Array(response.data)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        );
        return `data:${response.headers['content-type'].toLowerCase()};base64,${image}`;
      });

이것이 μž‘λ™ν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ” axiosκ°€ λͺ¨λ“  것을 json 객체에 μ €μž₯ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. λ°”μ΄λ„ˆλ¦¬λ₯Ό json 객체에 넣을 수 μ—†κΈ° λ•Œλ¬Έμ— λ°”μ΄λ„ˆλ¦¬λ₯Ό κΉ¨λŠ” λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.

이것은 λ‚˜λ₯Ό μœ„ν•΄ μ™„λ²½ν•˜κ²Œ μž‘λ™ν–ˆμŠ΅λ‹ˆλ‹€.

function getBase64(url) {
  return axios
    .get(url, {
      responseType: 'arraybuffer'
    })
    .then(response => new Buffer(response.data, 'binary').toString('base64'))
}

λ‚˜μ™€ 같은 λ…Έλ“œμ— 있고 v >= 6 을 μ‚¬μš©ν•˜λŠ” 경우 λ³€κ²½ 사항이 μžˆμœΌλ―€λ‘œ Buffer.from λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

function getBase64(url) {
  return axios
    .get(url, {
      responseType: 'arraybuffer'
    })
    .then(response => Buffer.from(response.data, 'binary').toString('base64'))
}

μ°Έμ‘° μ•ˆμ „ 버퍼
new Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead

@sean-hill μ΄λ―Έμ§€μ˜ 첫 번째 토큰을 얻을 수 μ—†λŠ” μ΄μœ κ°€ 무엇인지 μ•„μ‹­λ‹ˆκΉŒ? μ–΄λ–€ 이유둜 data:image/png;base64, 뢀뢄이 ν‘œμ‹œλ˜μ§€ μ•Šμ•„ base64 데이터가 μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‚˜λ₯Ό μœ„ν•œ 이 μž‘ν’ˆ

function getImage(imageUrl) {
var options = {
    url: `${imageUrl}`,
    encoding: "binary"
};
  return new Promise(function (resolve, reject) {
    request.get(options, function (err, resp, body) {
        if (err) {
            reject(err);
        } else {
            var prefix = "data:" + resp.headers["content-type"] + ";base64,";
            var img = new Buffer(body.toString(), "binary").toString("base64");
            //  var img = new Buffer.from(body.toString(), "binary").toString("base64");
            var dataUri = prefix + img;
            resolve(dataUri);
        }
    })
})
}
function getImage(imageUrl) {
var options = {
    url: `${imageUrl}`,
    encoding: "binary"
};

    request.get(options, function (err, resp, body) {
        if (err) {
            reject(err);
        } else {
            var prefix = "data:" + resp.headers["content-type"] + ";base64,";
            var img = new Buffer(body.toString(), "binary").toString("base64");
           //  var img = new Buffer.from(body.toString(), "binary").toString("base64");
            var dataUri = prefix + img;
        }
    })
}
return axios.get('http://site.com/img.png', { responseType: 'arraybuffer' })
  .then(response => `data:${response.headers['content-type']};base64,${btoa(String.fromCharCode(...new Uint8Array(response.data)))}`);
return axios.get('http://site.com/img.png', { responseType: 'arraybuffer' })
  .then(response => `data:${response.headers['content-type']};base64,${btoa(String.fromCharCode(...new Uint8Array(response.data)))}`);

λ…Έλ“œ 버전:

    return axios.get(url, { responseType: 'arraybuffer' }).then(res => {
      ;`data:${res.headers['content-type']};base64,${Buffer.from(String.fromCharCode(...new Uint8Array(res.data)), 'binary')
        .toString('base64')}`
    })

@voidpls
κ·€ν•˜μ˜ 방법은 였λ₯˜λ₯Ό μœ λ°œν•©λ‹ˆλ‹€!
Uncaught RangeError: Maximum call stack size exceeded

그것은 원인
toa(String.fromCharCode(...new Uint8Array(response.data)))

이 사진을 μ‹œλ„ ν•  수 μžˆμŠ΅λ‹ˆλ‹€
texture 4a95078a

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰