Open demiantriebl opened 8 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)
What is "drawCanvas"? Is it your custom function?
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;
}
how can compress jpg and png files?