Skip to content

Instantly share code, notes, and snippets.

@jkosoy
Created November 19, 2012 19:59
Show Gist options
  • Save jkosoy/4113485 to your computer and use it in GitHub Desktop.
Save jkosoy/4113485 to your computer and use it in GitHub Desktop.
HTML5 Image Resize
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