首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 9:上传图像时如何将HEIF文件格式转换为已知的web格式

Angular 9:上传图像时如何将HEIF文件格式转换为已知的web格式
EN

Stack Overflow用户
提问于 2020-11-05 18:16:45
回答 2查看 1.2K关注 0票数 1

我正在管理一个用Angular 9编写的and应用程序(PWA),其中用户上传图像和裁剪,旋转等在裁剪。

在iOS上,一种新的图像格式(HEIF)正在成为标准,这些用户抱怨他们无法上传自己的照片。似乎有时iOS会自动转换成jpg格式,有时不会。因此,我们需要能够接收HEIF格式的图像。

我尝试将mimetype image/heif添加到accept属性中,但iOS上的*.heic图像仍然变暗。我似乎很多人只是选择接受所有文件,但这不是这个web应用程序的选项。

另外,cropperjs不支持HEIF图像格式,那么我们如何将其转换为已知的web格式?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-05 18:16:45

我们的解决方案是安装heic2any

代码语言:javascript
复制
npm install heic2any

然后在需要的组件中导入:

代码语言:javascript
复制
import heic2any from "heic2any";

accept属性中,添加file类型和文件扩展名。

代码语言:javascript
复制
image/jpeg, image/png, image/heif, .heic, .heif

如果您需要支持任何类型的图像上传:

代码语言:javascript
复制
image/*, .heic, .heif

我们使用的是ngfSelect,它看起来像这样(简化):

代码语言:javascript
复制
<div ngfSelect
  [accept]  = "'image/*, .heic, .heif'"
  (filesChange) = "imageSelected($event)">

下面是imageSelected()函数的简化版本

代码语言:javascript
复制
public imageSelected(event) {
  let f:File;

  //RECEIVE IMAGE

  if (event[0] && (event[0]['lastModified'] || event[0]['lastModifiedDate'])) {
    f = event[0];
    if (event.length > 1) {
      event.splice(0,event.length-1);
      f = event[0];
    }
  } else if (event.target && event.target.files && event.target.files[0]) {
    f = event.target.files[0];
  }

  if (!f) {
    //Handle error and exit
  }

  let blob:Blob = f;
  let file:File = f;

  let convProm:Promise<any>;
  
  //CONVERT HEIC TO JPG

  if (/image\/hei(c|f)/.test(f.type)) {
    convProm = heic2any({blob,toType:"image/jpeg",quality:0}).then((jpgBlob:Blob) => {

      //Change the name of the file according to the new format
      let newName = f.name.replace(/\.[^/.]+$/, ".jpg");

      //Convert blob back to file
      file = this.blobToFile(jpgBlob,newName);

    }).catch(err => {
      //Handle error
    });
  } else {
    //This is not a HEIC image so we can just resolve
    convProm = Promise.resolve(true);
  }

  //ADD IMAGE FILE TO CROPPERJS EDITOR

  convProm.then(() => {
    
    let reader = new FileReader();
    let _thisComp = this;

    //Add file to FileReader
    if (file) {
      reader.readAsDataURL(file);
    }
    //Listen for FileReader to get ready
    reader.onload = function () {
      
      //Set imageUrl. This will trigger initialization of cropper via imageLoaded() 
      //as configured in the html img tag:
      //<img #image id="image" [src]="imageUrl" (load)="imageLoaded($event)" class="cropper"> 

      _thisComp.imageUrl = reader.result;
    }
  });
}


private blobToFile = (theBlob: Blob, fileName:string): File => {
  let b: any = theBlob;

  //A Blob() is almost a File() - it's just missing the two properties below which we will add
  b.lastModified = new Date();
  b.name = fileName;

  //Cast to a File() type
  return <File>theBlob;
}

以上可能不是最好的解决方案,但我希望它能对其他面临同样挑战的人有所帮助和启发。

票数 4
EN

Stack Overflow用户

发布于 2021-04-23 07:09:45

heic2any只在客户端工作;这意味着如果你使用heic2any,你可能会破坏angular build - pre-render,并且你将面临刷新路由问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64695144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档