powerbot15/image-compressor

How to use it in React. "Error: Source can not be loaded"

bluesealjs opened this issue · 2 comments

I want to use this module in react. However, I am getting error Error: Source can not be loaded. I installed via npm install image-compressor

import "image-compressor";
var ImageCompressor = new window.ImageCompressor();

var compressorSettings = {
  toWidth: 100,
  toHeight: 100,
  mimeType: "image/png",
  mode: "strict",
  quality: 0.6,
  grayScale: true,
  sepia: true,
  threshold: 127,
  vReverse: true,
  hReverse: true,
  speed: "low"
};

const proceedCompressedImage = compressedSrc => {
  console.log("Compressed Src: ", compressedSrc);
};

export const compressImg = imageSrc => {
  ImageCompressor.run(imageSrc, compressorSettings, proceedCompressedImage);
};

Sorry man, this version is in ES5 syntax and can not be loaded within ES6 import statement. You should setup webpack in your application so it will add this script "as is" to the build file. Then you can simply use it as window.ImageCompressor.
By the way, I have a plans to update ImageCompressor to be available within ES6 import.

ES6 version has been published. Update your image-compressor to 2.0.2 and then you can use it through import { ImageCompressor } from 'image-compressor'