3 $.fn.aeImageResize = function( params ) {
6 // Nasty I know but it's done only once, so not too bad I guess
7 // Alternate suggestions welcome :)
8 , isIE6 = $.browser.msie && (6 == ~~ $.browser.version)
11 // We cannot do much unless we have one of these
12 if ( !params.height && !params.width ) {
16 // Calculate aspect ratio now, if possible
17 if ( params.height && params.width ) {
18 aspectRatio = params.width / params.height;
21 // Attach handler to load
22 // Handler is executed just once per element
23 // Load event required for Webkit browsers
24 return this.one( "load", function() {
26 // Remove all attributes and CSS rules
27 this.removeAttribute( "height" );
28 this.removeAttribute( "width" );
29 this.style.height = this.style.width = "";
31 var imgHeight = this.height
32 , imgWidth = this.width
33 , imgAspectRatio = imgWidth / imgHeight
34 , bxHeight = params.height
35 , bxWidth = params.width
36 , bxAspectRatio = aspectRatio;
39 // If one parameter is missing, we just force calculate it
40 if ( !bxAspectRatio ) {
42 bxAspectRatio = imgAspectRatio + 1;
44 bxAspectRatio = imgAspectRatio - 1;
48 // Only resize the images that need resizing
49 if ( (bxHeight && imgHeight > bxHeight) || (bxWidth && imgWidth > bxWidth) ) {
51 if ( imgAspectRatio > bxAspectRatio ) {
52 bxHeight = ~~ ( imgHeight / imgWidth * bxWidth );
54 bxWidth = ~~ ( imgWidth / imgHeight * bxHeight );
57 this.height = bxHeight;
63 // Trigger load event (for Gecko and MSIE)
64 if ( this.complete || isIE6 ) {
65 $( this ).trigger( "load" );