]> git.mxchange.org Git - friendica.git/blob - view/theme/cleanzero/js/jquery.ae.image.resize.js
only use Smarty if the view/smarty3/ directory is writable; location bug fix
[friendica.git] / view / theme / cleanzero / js / jquery.ae.image.resize.js
1 (function( $ ) {
2
3   $.fn.aeImageResize = function( params ) {
4
5     var aspectRatio = 0
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)
9       ;
10
11     // We cannot do much unless we have one of these
12     if ( !params.height && !params.width ) {
13       return this;
14     }
15
16     // Calculate aspect ratio now, if possible
17     if ( params.height && params.width ) {
18       aspectRatio = params.width / params.height;
19     }
20
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() {
25
26       // Remove all attributes and CSS rules
27       this.removeAttribute( "height" );
28       this.removeAttribute( "width" );
29       this.style.height = this.style.width = "";
30
31       var imgHeight = this.height
32         , imgWidth = this.width
33         , imgAspectRatio = imgWidth / imgHeight
34         , bxHeight = params.height
35         , bxWidth = params.width
36         , bxAspectRatio = aspectRatio;
37                                 
38       // Work the magic!
39       // If one parameter is missing, we just force calculate it
40       if ( !bxAspectRatio ) {
41         if ( bxHeight ) {
42           bxAspectRatio = imgAspectRatio + 1;
43         } else {
44           bxAspectRatio = imgAspectRatio - 1;
45         }
46       }
47
48       // Only resize the images that need resizing
49       if ( (bxHeight && imgHeight > bxHeight) || (bxWidth && imgWidth > bxWidth) ) {
50
51         if ( imgAspectRatio > bxAspectRatio ) {
52           bxHeight = ~~ ( imgHeight / imgWidth * bxWidth );
53         } else {
54           bxWidth = ~~ ( imgWidth / imgHeight * bxHeight );
55         }
56
57         this.height = bxHeight;
58         this.width = bxWidth;
59       }
60     })
61     .each(function() {
62
63       // Trigger load event (for Gecko and MSIE)
64       if ( this.complete || isIE6 ) {
65         $( this ).trigger( "load" );
66       }
67     });
68   };
69 })( jQuery );