source: extensions/GThumb/js/gthumb.js @ 14974

Last change on this file since 14974 was 13652, checked in by patdenice, 12 years ago

Compatible with Piwigo 2.4

File size: 6.5 KB
Line 
1var GThumb = {
2
3  max_height: 200,
4  margin: 10,
5  max_first_thumb_width: 0.7,
6  big_thumb: null,
7  small_thumb: null,
8  method: 'crop',
9  t: new Array,
10
11  build: function () {
12
13    GThumb.t = new Array;
14    jQuery('#thumbnails img.thumbnail').each(function(index) {
15      width = parseInt(jQuery(this).attr('width'));
16      height = parseInt(jQuery(this).attr('height'));
17      th = {index:index,width:width,height:height,real_width:width,real_height:height};
18      if (height < GThumb.max_height) {
19        th.width = Math.round(GThumb.max_height * width / height);
20        th.height = GThumb.max_height;
21      }
22      GThumb.t.push(th);
23    });
24
25    first = GThumb.t[0];
26    GThumb.small_thumb = {index:first.index,width:first.real_width,height:first.real_height,src:jQuery('#thumbnails img:first').attr('src')}
27
28    jQuery.resize.throttleWindow = false;
29    jQuery.resize.delay = 50;
30    GThumb.process();
31  },
32
33  process: function() {
34
35    var width_count = GThumb.margin;
36    var line = 1;
37    var round_rest = 0;
38    var main_width = jQuery('#thumbnails').width();
39    var first_thumb = jQuery('#thumbnails img:first');
40    var best_size = {width:1,height:1};
41
42    if (GThumb.big_thumb != null && GThumb.big_thumb.height < main_width * GThumb.max_first_thumb_width) {
43
44      // Compute best size for landscape picture (we choose bigger height)
45      min_ratio = Math.min(1.05, GThumb.big_thumb.width/GThumb.big_thumb.height);
46
47      for(width = GThumb.big_thumb.width; width/best_size.height>=min_ratio; width--) {
48        width_count = GThumb.margin;
49        height = GThumb.margin;
50        max_height = 0;
51        available_width = main_width - (width + GThumb.margin);
52        line = 1;
53        for (i=1;i<GThumb.t.length;i++) {
54
55          width_count += GThumb.t[i].width + GThumb.margin;
56          max_height = Math.max(GThumb.t[i].height, max_height);
57
58          if (width_count > available_width) {
59            ratio = width_count / available_width;
60            height += Math.round(max_height / ratio);
61            line++;
62            max_height = 0;
63            width_count = GThumb.margin;
64            if (line > 2) {
65              if (height >= best_size.height && width/height >= min_ratio && height<=GThumb.big_thumb.height) {
66                best_size = {width:width,height:height};
67              }
68              break;
69            }
70          }
71        }
72        if (line <= 2) {
73          if (max_height == 0 || line == 1) {
74            height = GThumb.big_thumb.height;
75          } else {
76            height += max_height;
77          }
78          if (height >= best_size.height && width/height >= min_ratio && height<=GThumb.big_thumb.height) {
79            best_size = {width:width,height:height}
80          }
81        }
82      }
83
84      if (GThumb.big_thumb.src != first_thumb.attr('src')) {
85        first_thumb.attr('src', GThumb.big_thumb.src).attr({width:GThumb.big_thumb.width,height:GThumb.big_thumb.height});
86        GThumb.t[0].width = GThumb.big_thumb.width;
87        GThumb.t[0].height = GThumb.big_thumb.height;
88      }
89      GThumb.t[0].crop = best_size.width;
90      GThumb.resize(first_thumb, GThumb.big_thumb.width, GThumb.big_thumb.height, best_size.width, best_size.height, true);
91
92    }
93
94    if (best_size.width == 1) {
95      if (GThumb.small_thumb != null && GThumb.small_thumb.src != first_thumb.attr('src')) { 
96        first_thumb.prop('src', GThumb.small_thumb.src).attr({width:GThumb.small_thumb.width,height:GThumb.small_thumb.height});
97        GThumb.t[0].width = GThumb.small_thumb.width;
98        GThumb.t[0].height = GThumb.small_thumb.height;
99      }
100      GThumb.t[0].crop = false;
101    }
102
103    width_count = GThumb.margin;
104    max_height = 0;
105    line = 1;
106    thumb_process = new Array;
107
108    for (i=GThumb.t[0].crop!=false?1:0;i<GThumb.t.length;i++) {
109
110      width_count += GThumb.t[i].width + GThumb.margin;
111      max_height = Math.max(GThumb.t[i].height, max_height);
112      thumb_process.push(GThumb.t[i]);
113
114      available_width = main_width;
115      if (line <= 2 && GThumb.t[0].crop !== false) {
116        available_width -= (GThumb.t[0].crop + GThumb.margin);
117      }
118
119      if (width_count > available_width) {
120
121        last_thumb = GThumb.t[i].index;
122        ratio = width_count / available_width;
123        new_height = Math.round(max_height / ratio);
124        round_rest = 0;
125        width_count = GThumb.margin;
126
127        for (j=0;j<thumb_process.length;j++) {
128
129          if (thumb_process[j].index == last_thumb) {
130            new_width = available_width - width_count - GThumb.margin;
131          } else {
132            new_width = (thumb_process[j].width + round_rest) / ratio;
133            round_rest = new_width - Math.round(new_width);
134            new_width = Math.round(new_width);
135          }
136          GThumb.resize(jQuery('#thumbnails img.thumbnail').eq(thumb_process[j].index), thumb_process[j].real_width, thumb_process[j].real_height, new_width, new_height, false);
137
138          width_count += new_width + GThumb.margin;
139        }
140        thumb_process = new Array;
141        width_count = GThumb.margin;
142        max_height = 0;
143        line++;
144      }
145    }
146
147    // Last line does not need to be cropped
148    for (j=0;j<thumb_process.length;j++) {
149      GThumb.resize(jQuery('#thumbnails img.thumbnail').eq(thumb_process[j].index), thumb_process[j].real_width, thumb_process[j].real_height, thumb_process[j].width, max_height, false);
150    }
151
152    if (main_width != jQuery('#thumbnails').width()) {
153      GThumb.process();
154    }
155  },
156
157  resize: function(thumb, width, height, new_width, new_height, is_big) {
158
159    if (GThumb.method == 'resize' || height < new_height || width < new_width) {
160      real_width = new_width;
161      real_height = new_height;
162      width_crop = 0;
163      height_crop = 0;
164
165      if (is_big) {
166        if (width - new_width > height - new_height) {
167          real_width = Math.round(new_height * width / height);
168          width_crop = Math.round((real_width - new_width)/2);
169        } else {
170          real_height = Math.round(new_width * height / width);
171          height_crop = Math.round((real_height - new_height)/2);
172        }
173      }
174      thumb.css({
175        height: real_height+'px',
176        width: real_width+'px'
177      });
178    } else {
179      thumb.css({height: '', width: ''});
180      height_crop = Math.round((height - new_height)/2);
181      width_crop = Math.round((width - new_width)/2);
182    }
183
184    thumb.parents('li').css({
185      height: new_height+'px',
186      width: new_width+'px'
187    });
188    thumb.parent('a').css({
189      clip: 'rect('+height_crop+'px, '+(new_width+width_crop)+'px, '+(new_height+height_crop)+'px, '+width_crop+'px)',
190      top: -height_crop+'px',
191      left: -width_crop+'px'
192    });
193  }
194}
Note: See TracBrowser for help on using the repository browser.