简体   繁体   English

我可以将src img属性指定为Blob文件还是以base64编码的图像?

[英]Can i apoint the src img attribute to a blob file or a base64 encoded image?

I was wonder if somehow i could get the image content of a url and transform it into a base64 or a blob file and append it to the src attribute ? 我想知道是否能以某种方式获取url的图像内容并将其转换为base64或blob文件并将其附加到src属性?

And if this is possible how can i do this in the angular way ? 如果可能的话,我该怎么做呢? I tried to do something like this and its not working 我试图做这样的事情,它不起作用

    const fileReader = new FileReader();
    let userAvatar: string;
    this.avatarSubject = new BehaviorSubject(this.userService.getAvatar(this.channelId));
    this.avatarSubject.subscribe(res => {
      userAvatar = res;
      const binaryImg = atob(userAvatar);
      const length = binaryImg.length;
      const buffer = new ArrayBuffer(length);
      const uint = new Uint8Array(buffer);
      const blob = new Blob([uint], {type: 'image/jpeg'});
      fileReader.readAsDataURL(blob);
    }).unsubscribe();

You can set an image src to a Base64 dataURI, not to a blob though. 您可以将图片src设置为Base64 dataURI,而不是blob。

You can retrieve the image as a blob, then convert it to a dataURI, if it doesn't violate any cors policy. 您可以将图片检索为Blob,然后将其转换为dataURI(如果它不违反任何cors策略)。

(async ()=>{
  var url = 'https://imgur.com/gallery/rh5O7wN';
  var blob = await urlToBlob(url);
  var datauri = await blobToDataURI(blob);
  document.getElementById('img').src = datauri;
})();

function urlToBlob(url) {
  return new Promise(done => {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.responseType = 'arraybuffer';
    xhr.addEventListener('load', function() {
      if (xhr.status === 200) done(new Blob([xhr.response]));
    })
    xhr.send();
  });
}

function blobToDataURI(blob) {
  return new Promise(done => {
    var a = new FileReader();
    a.onload = e => done(e.target.result);
    a.readAsDataURL(blob);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图像从 NodeJS 缓冲区 object 转换为 JavaScript 中的 base64 以便它可以在<img> src 属性? - How do I convert an image from a NodeJS Buffer object to base64 in JavaScript so it can be used in the <img> src attribute? 将图像标记的src属性中的变量值设置为javascript / ajax中的base64编码图像 - setting variable value in src attribute of image tag to a base64 encoded image in javascript/ajax 我可以处理base64编码图像的水印吗? - Can I process base64 encoded image for watermark? IMG不显示编码为Base64字符串的PNG图像 - IMG not showing a PNG image encoded as a Base64 string Blob到image / webp类型的base64编码的DataURI - Blob to base64 encoded DataURI of type image/webp 如何转换 HTML<img> 标记到 Base64 编码的图像文件中? - How to convert an HTML <img> tag into a Base64 encoded image file? 如何在 img src 的 a.txt 文件中显示 base64 图像 - How to display a base64 image that is inside a .txt file on the img src 如何返回我可以使用的 Base64<img src="renderedImg(photo)"> - How to return Base64 which I can use with <img src="renderedImg(photo)"/> 如何将base64编码的图像异步发布为文件? - How to post a base64 encoded image as a file asynchronously? 我可以使src属性从服务器或调整服务器中获取base64数据吗? - Can I make the src attribute fetch base64 data from a server or adjusting server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM