question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to convert HEIC Image to JPEG using custom plugin

See original GitHub issue

Here I have created custom plugin to convert HEIC Image to JPEG using custom plugin. In which install method is called and I am getting info logs but not getting any info logs from from prepareUpload method.

here is the code

import { Plugin } from "@uppy/core";
import convert from "heic-convert";

class UppyHEICImageConversion extends Plugin {
  constructor(uppy, opts) {
    super(uppy, opts);
    this.id = this.opts.id || "HEICImageConversion";
    this.type = "modifier";

    this.prepareUpload = this.prepareUpload.bind(this);
    this.conversion = this.conversion.bind(this);
  }

  async conversion(currentFile) {
    if (
      currentFile.name.split(".").pop() === "HEIC" ||
      currentFile.name.split(".").pop() === "heic"
    ) {
      let base64String = new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(currentFile.data);
        reader.onload = () => resolve(reader.result);
        reader.onerror = (error) => reject(error);
      });

      base64String = base64String.split(";base64,")[1];
      let inputBuffer = Buffer.from(base64String, "base64");

      const outputBuffer = await convert({
        buffer: inputBuffer, // the HEIC file buffer
        format: "JPEG", // output format
        quality: 1, // the jpeg compression quality, between 0 and 1
      });

      currentFile.convertHEICToJPG = outputBuffer;

      if (!currentFile.convertHEICToJPG) {
        return false;
      } else {
        let blob = new Blob([currentFile.convertHEICToJPG]);
        currentFile.data = new File([blob], "abc.jpg", {
          type: "image/jpeg",
        });
      }
    }
    return currentFile;
  }

  prepareUpload(fileIDs) {
    this.uppy.info("prepareUpload called!", "success", 3000);
    const promises = fileIDs.map(async (fileID) => {
      const file = this.uppy.getFile(fileID);

      if (file.type.split("/")[0] !== "image") {
        return;
      }

      return this.conversion(file)
        .then((jpegImage) => {
          this.uppy.setFileState(fileID, { data: jpegImage });
        })
        .catch((err) => {
          this.uppy.log(
            `HEIC Image conversion to JPEG Failed ${file.id}:`,
            "warning"
          );
          this.uppy.log(err, "warning");
        });
    });

    const emitPreprocessCompleteForAll = () => {
      fileIDs.forEach((fileID) => {
        const file = this.uppy.getFile(fileID);
        this.uppy.emit("preprocess-complete", file);
      });
    };

    // Why emit `preprocess-complete` for all files at once, instead of
    // above when each is processed?
    // Because it leads to StatusBar showing a weird “upload 6 files” button,
    // while waiting for all the files to complete pre-processing.
    return Promise.all(promises).then(emitPreprocessCompleteForAll);
  }

  install() {
    this.uppy.info("Oh my, something good happened!", "success", 3000);
    this.uppy.addPreProcessor(this.prepareUpload);
  }
}

export default UppyHEICImageConversion;

P.S I am using this plugin so that I can enable HEIC preview in uppy as converting it into jpeg and able to edit also using image editor

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Murderloncommented, Jun 14, 2021

@nisha-parikh this is a limitation in the library we are using (see https://github.com/transloadit/uppy/pull/2838#issuecomment-855801123). But if I’m correct, you should still be able to control zoom with the zoom (magnifying glass icon) buttons.

0reactions
Murderloncommented, Aug 10, 2021

This question about your custom plugin seems to be solved. Regarding the CSS selector, I’m not sure what kind of effect you want to implement so it’s hard to pin point. Closing this, but discussion can continue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert HEIC images to JPEG on Upload - WordPress.org
If your host has HEIC support in imagick (check phpinfo()) then its possible to create a custom plugin with set of hooks to...
Read more >
Multiple Methods to Convert HEIC to JPG on Windows 10
If you want your HEIC files in another format, the following guide will show you how you can convert HEIC to JPG in...
Read more >
How to convert HEIC to JPEG on a PC - Digital Trends
How to convert HEIC to JPEG in Windows · Step 1: Open your file in the Windows Photo app · Step 2: Click...
Read more >
How to convert HEIC to JPG: The ultimate 2022 guide - Setapp
Learn how to change HEIC to JPG on Mac using Apple's native apps and quick one-click tools. Build your perfect image conversion workflow....
Read more >
Converting .HEIC to JPEG using imagick in C# - Stack Overflow
Read first frame of gif image using (MagickImage image = new ... Jpeg; // Create byte array that contains a jpeg file byte[]...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found