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

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

Bug corrected with special images (like 3d pictures).
Work with question mark in urls = false.

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