D
D
Dmitry Vitvitsky2017-10-19 17:43:49
PHP
Dmitry Vitvitsky, 2017-10-19 17:43:49

How to convert heic format to jpg using js or php?

Guys, has anyone already tried to convert these formats?
I'm trying to convert heic format to one of the popular ones (jpg, png). I do everything as in the official repository of this format:

Reader download
HEIFReader reader = new HEIFReader('test.heic');
reader.requestFileInfo(function(payload) {
  if(payload.success !== true) {
    console.error("Could not read file:", url);
  } else {
    var fileInfo = payload;
    console.log("FileInfo contents:", fileInfo);
  }
});
Getting data from a heic file
// META file containing still images
if (fileInfo.rootLevelMetaBoxProperties) {
  var masterContextId = fileInfo.rootLevelMetaBoxProperties.contextId;
  var masterItemIds = [];
  var imageFeaturesMap = fileInfo.rootLevelMetaBoxProperties.imageFeaturesMap;
  for (i in imageFeaturesMap) {
    if (imageFeaturesMap.hasOwnProperty(i) && imageFeaturesMap[i].isMasterImage === true) {
      masterItemIds.push(parseInt(i));
    }
  }

  console.log("Master images in the file:", masterItemIds);
}
Decoding a heic file
/** Request decoded image data for given item id's.
 *  @param {number} contextId Id of the context.
 *  @param {Array.<number>} itemIds Array of item id's to be decoded.
 *  @param {function} callback Callback function that receives the payload as a parameter. */
this.requestImageData = function (contextId, itemIds, callback) {
  console.log(_name + ": REQUEST IMAGE DATA: " + itemIds);

  if (itemIds.constructor !== Array) {
    itemIds = [itemIds];
  }

  if (itemIds.length < 1) {
    return;
  }

  var stream = new Uint8Array();
  var decodeIds = [];
  var dependencies = {};
  var displayDependencies = {};
  for (var i = 0; i < itemIds.length; i++) {
    var refs = self._heifReader.getItemDecodeDependencies(contextId, itemIds[i]);
    for (var j = 0; j < refs.length; j++) {
      if (refs[j] !== itemIds[i]) {
        // Add dependency data to the stream if the id is not already listed in dependencies
        if(!(refs[j] in dependencies) && !(refs[j] in displayDependencies)) {
          dependencies[refs[j]] = true;
          stream = _appendBuffer(stream, new Uint8Array(self._heifReader.getItemDataWithDecoderParameters(contextId, refs[j])));
          decodeIds.push(refs[j]);
        }
      } else {
        // Item has itself as dependency => we want to display that item despite being a dependency
        displayDependencies[refs[j]] = true;
      }
    }
  }

  // Add data of the actual items to the stream (that already contains dependency data)
  for (i = 0; i < itemIds.length; i++) {
    stream = _appendBuffer(stream, new Uint8Array(self._heifReader.getItemDataWithDecoderParameters(contextId, itemIds[i])));
    decodeIds.push(itemIds[i]);
  }

  var payload = new Payload(true, [], 0, 0);
  self._hevcDecoder.pushRequestContext(new RequestContext(callback, decodeIds, stream, dependencies, payload));
  self._hevcDecoder.decode();
};

The strangest thing starts in the line
self._hevcDecoder.decode();
The Wiki of this repository says:
HevcDecoder decodes hevc encoded stream to RGBA image data.

But basically I am not getting the RGBA dataset. I am getting a HevcDecoder() object. But how do I get my data out of there.
If anyone has tried to convert, please help. I've been sitting on this for a week now

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question