Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heic format conversion via heic2any #71

Open
DiegoWtf opened this issue May 27, 2021 · 3 comments
Open

Heic format conversion via heic2any #71

DiegoWtf opened this issue May 27, 2021 · 3 comments

Comments

@DiegoWtf
Copy link

Hi.
Has anyone done such a feature? Can you share a working code?
https://github.com/alexcorvi/heic2any

@EBakirdinov
Copy link

@DiegoWtf doing such stuff right now, did almost everything except replacing heic image by jpeg (converting to it) and saving, have u succeed? Wanna just get some advices...

@EBakirdinov
Copy link

EBakirdinov commented May 13, 2022

Managed to fix all the issues, here is my code.

function convertheic2jpeg(blob, fileNameFull) {
  heic2any({
    blob: blob,
    toType: "image/jpeg"
  }).then(function (resultBlob) {
    var myFile = new File([resultBlob], fileNameFull.replace(/\.[^/.]+$/, "") + '.jpeg', {
      type: resultBlob.type,
    });
    var files = api.getFiles();
    for (let i = 0; i < files.length; i++) {
      var elCurrent = files[i];
      if (elCurrent.name.replace(/\.[^/.]+$/, "") == myFile['name'].replace(/\.[^/.]+$/, "")) {   
        api.update(elCurrent, {choosed: true, file: myFile, extension: "jpeg", type: resultBlob.type, name: elCurrent['title'] + ".jpeg"});
        files[i]['choosed'] = true;
        api.updateFileList();
        break;
      };
    };
  }).catch(function (x) {
    console.log([x.code, x.message]);
  });
};

"FileNameFull" var is the name of the file with !!HEIC extension!!

FileUploader has callback "beforeSelect", there i call heic2any function. If u want to add some loader u need "onSelect" callback.

beforeSelect: function(files, listEl, parentEl, newInputEl, inputEl) {
  for (let i = 0; i < files.length; i++) {
    var file = files[i];
    var fileFullName = file['name'];
    var fileExt = fileFullName.substr(fileFullName.lastIndexOf('.') + 1);
    var fileName = fileFullName.replace(/\.[^/.]+$/, "");

    if (fileExt.toLowerCase() == "heic") {
      var r = (Math.random() + 1).toString(36).substring(6);

      Object.defineProperty(files[i], 'name', {
        writable: true,
        value: `${fileName}${r}.${fileExt}`
      });

      convertheic2jpeg(files[i], files[i]['name']);
    }
  }
  return true;
},

@EBakirdinov
Copy link

@innostudio i think we can close this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants