javascript resize image base64
function resizeImage(base64Str, maxWidth = 400, maxHeight = 350) {
return new Promise((resolve) => {
let img = new Image()
img.src = base64Str
img.onload = () => {
let canvas = document.createElement('canvas')
const MAX_WIDTH = maxWidth
const MAX_HEIGHT = maxHeight
let width = img.width
let height = img.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
let ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, width, height)
resolve(canvas.toDataURL())
}
})
}
Also in JavaScript:
- mongodb delete duplicate documents
- javascript is undefined
- javascript use camera
- loopback find or create
- async sleep javascript
- underscore js join
- js takes a string of words and returns the highest scoring word.
- react hook s
- js check if number is divisible by 2
- creating a nested loop of a chessboard in javascript
- react keep screen on
- how to check palindrome in javascript for lowercase and uppercase
- js loop through associative array
- jquery closest
- how to set default value in input field in angularjs
- how to do radio button validation in jquery
- javascript loop through object
- jquery select element based on for attribute
- setting property to method in child class javascript
- standalone form inside reactive form
- javascript collision detection
- javascript string concatenation
- npl live server doesn't load js
- unique array javascript es6 Map