Created
July 21, 2020 14:45
-
-
Save thuliumsystems/c44da7ecc77daa4676a8aa268e10a638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async fn_inserir_pic_pessoal() { | |
const image = await Plugins.Camera.getPhoto({ | |
quality: 50, | |
allowEditing: false, | |
resultType: CameraResultType.DataUrl, | |
source: CameraSource.Prompt | |
}); | |
this.fn_resize_img(image.dataUrl, 1024, 1024, 0.5, f => { | |
this.data_profile.user_pic = f | |
}) | |
} | |
public fn_resize_img(img, MAX_WIDTH: number = 640, MAX_HEIGHT: number = 640, quality: number = 1, callback) { | |
var canvas: any = document.createElement("canvas"); | |
var image = new Image(); | |
image.onload = () => { | |
var width = image.width; | |
var height = image.height; | |
if (width > height) { | |
if (width > MAX_WIDTH) { | |
height *= MAX_WIDTH / width; | |
width = MAX_WIDTH; | |
} | |
} else { | |
if (height > MAX_HEIGHT) { | |
width *= MAX_HEIGHT / height; | |
height = MAX_HEIGHT; | |
} | |
} | |
canvas.width = width; | |
canvas.height = height; | |
var ctx = canvas.getContext("2d"); | |
ctx.drawImage(image, 0, 0, width, height); | |
// IMPORTANT: 'jpeg' NOT 'jpg' | |
var dataUrl = canvas.toDataURL('image/jpeg', quality); | |
callback(dataUrl) | |
} | |
image.src = img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment