Created
August 24, 2012 00:33
-
-
Save alejandrobernardis/3444003 to your computer and use it in GitHub Desktop.
JavaScript Simple Image Loader
This file contains hidden or 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 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