Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created November 15, 2022 17:03
Show Gist options
  • Save andreasvirkus/a813132542b38ff421aa0b83cc27e771 to your computer and use it in GitHub Desktop.
Save andreasvirkus/a813132542b38ff421aa0b83cc27e771 to your computer and use it in GitHub Desktop.

Revisions

  1. andreasvirkus created this gist Nov 15, 2022.
    13 changes: 13 additions & 0 deletions resizeImage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    function resizeImage(el, width, height, quality) {
    var canvas = document.createElement('canvas')
    canvas.width = width
    canvas.height = height
    var context = canvas.getContext('2d')
    context.drawImage(el, 0, 0, width, height)

    try {
    return canvas.toDataURL('image/jpeg', quality)
    } catch (e) {
    return null
    }
    }