Created
November 19, 2012 19:59
-
-
Save jkosoy/4113485 to your computer and use it in GitHub Desktop.
HTML5 Image Resize
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
function resizeImage($img,$w,$h) { | |
var maxWidth = $w; | |
var maxHeight = $h; | |
var w = $img.width; | |
var h = $img.height; | |
if(w > h) { | |
if(w > maxWidth) { | |
h *= maxWidth/w; | |
w = maxWidth; | |
} | |
} | |
else { | |
if(h > maxHeight) { | |
w *= maxHeight/h; | |
h = maxHeight; | |
} | |
} | |
// resize the image. | |
var canvas = document.createElement('canvas'); | |
canvas.width = w; | |
canvas.height = h; | |
var ctx = canvas.getContext('2d'); | |
ctx.drawImage($img,0,0,w,h); | |
return canvas.toDataURL(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment