CulturalMe / meteor-slingshot

Upload files directly to AWS S3, Google Cloud Storage and others in meteor
MIT License
595 stars 104 forks source link

Compress image #205

Open demiantriebl opened 8 years ago

demiantriebl commented 8 years ago

how can compress jpg and png files?

rnarayan commented 7 years ago

This is how I compress the image in one of my projects.

 let img = document.createElement("img")
    img.src = file  //file is the document(image) that user uploaded
    img.crossOrigin = "Anonymous"   //this line needed for Firefox
    let canvas = drawCanvas(img,400,400)  //400,400 is the dimension of the image I want

    let metaContext = {avatarId: userId}
    let uploader = new Slingshot.Upload("googleUploads", metaContext)

    canvas.toBlob(function(blob) {
      blob.name = 'myImage.jpg'
      uploader.send(blob, function (err, downloadUrl) {
        if (err) {
          console.log(`There was an error in uploading the image`)
        } else {
          console.log(`Image was successfully uploaded.`)
        }
      })
    }, "image/jpeg", 0.70)
rgnevashev commented 7 years ago

What is "drawCanvas"? Is it your custom function?

rnarayan commented 7 years ago

Yes. It's a custom function.

function drawCanvas(img,width,height) {
  let canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  let ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0, width, height);
  return canvas;
}