Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created August 24, 2012 00:33
Show Gist options
  • Select an option

  • Save alejandrobernardis/3444003 to your computer and use it in GitHub Desktop.

Select an option

Save alejandrobernardis/3444003 to your computer and use it in GitHub Desktop.
JavaScript Simple Image Loader
function load_image_profile(url, to, w, h){
if(!url || !to) return;
var img = new Image();
img.onload = function(){
var scope = $(to);
var max_h = h || 176,
max_w = w || 176,
ratio = this.height/this.width;
if(this.width > max_w) {
this.width = max_w;
this.height = Math.round(this.width*ratio);
if(this.height < max_h){
this.height = max_h;
this.width = Math.round(this.height/ratio);
}
} else if(this.height > max_h) {
this.height = max_h;
this.width = Math.round(this.height/ratio);
if(this.width < max_w){
this.width = max_w;
this.height = Math.round(this.width*ratio);
}
}
if(scope.children().length>1){
scope.children().remove(':first');
}
scope.prepend(this);
}
img.src = url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment