$.fn.imageResize = function(width, height) {
    if (width == null)  width = 100;
    if (height == null) height = 100;

    return this.each(function() {
        var img = $(this);
        var newWidth = img.width();
        var newHeight = img.height();
        
        if (newWidth > width) {
            var ratio = newWidth / width;
            
            var h = newHeight / ratio;
            img.width(width);
            
            if (h > height) {
                img.height(height);
                ratio = newHeight / height;
                
                newWidth = newWidth / ratio;
                img.width(newWidth);
            }
        }   
        else if (newHeight > height) {
            var ratio = newHeight / height;
            
            var w = newWidth / ratio;
            img.height(height);
            
            if (w > width) {
                img.width(width);
                ratio = newWidth / width;
                
                newHeight = newHeight / ratio;
                img.height(newHeight);
            }
        }     
    });        
};

