[12678] | 1 | var 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 | |
---|
[12710] | 11 | build: function () { |
---|
[12678] | 12 | |
---|
[12710] | 13 | GThumb.t = new Array; |
---|
[13652] | 14 | jQuery('#thumbnails img.thumbnail').each(function(index) { |
---|
[12678] | 15 | width = parseInt(jQuery(this).attr('width')); |
---|
| 16 | height = parseInt(jQuery(this).attr('height')); |
---|
[13652] | 17 | th = {index:index,width:width,height:height,real_width:width,real_height:height}; |
---|
[12696] | 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); |
---|
[12678] | 23 | }); |
---|
| 24 | |
---|
[13652] | 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 | |
---|
[12678] | 28 | jQuery.resize.throttleWindow = false; |
---|
| 29 | jQuery.resize.delay = 50; |
---|
[12710] | 30 | GThumb.process(); |
---|
[12678] | 31 | }, |
---|
| 32 | |
---|
| 33 | process: function() { |
---|
| 34 | |
---|
[12710] | 35 | var width_count = GThumb.margin; |
---|
[12678] | 36 | var line = 1; |
---|
| 37 | var round_rest = 0; |
---|
[12710] | 38 | var main_width = jQuery('#thumbnails').width(); |
---|
| 39 | var first_thumb = jQuery('#thumbnails img:first'); |
---|
[12678] | 40 | var best_size = {width:1,height:1}; |
---|
| 41 | |
---|
[12710] | 42 | if (GThumb.big_thumb != null && GThumb.big_thumb.height < main_width * GThumb.max_first_thumb_width) { |
---|
[12678] | 43 | |
---|
| 44 | // Compute best size for landscape picture (we choose bigger height) |
---|
[12710] | 45 | min_ratio = Math.min(1.05, GThumb.big_thumb.width/GThumb.big_thumb.height); |
---|
[12678] | 46 | |
---|
[12710] | 47 | for(width = GThumb.big_thumb.width; width/best_size.height>=min_ratio; width--) { |
---|
| 48 | width_count = GThumb.margin; |
---|
| 49 | height = GThumb.margin; |
---|
[12679] | 50 | max_height = 0; |
---|
[12710] | 51 | available_width = main_width - (width + GThumb.margin); |
---|
[12678] | 52 | line = 1; |
---|
[12710] | 53 | for (i=1;i<GThumb.t.length;i++) { |
---|
[12678] | 54 | |
---|
[12710] | 55 | width_count += GThumb.t[i].width + GThumb.margin; |
---|
| 56 | max_height = Math.max(GThumb.t[i].height, max_height); |
---|
[12678] | 57 | |
---|
| 58 | if (width_count > available_width) { |
---|
| 59 | ratio = width_count / available_width; |
---|
[12679] | 60 | height += Math.round(max_height / ratio); |
---|
[12678] | 61 | line++; |
---|
[12679] | 62 | max_height = 0; |
---|
[12710] | 63 | width_count = GThumb.margin; |
---|
[12678] | 64 | if (line > 2) { |
---|
[12710] | 65 | if (height >= best_size.height && width/height >= min_ratio && height<=GThumb.big_thumb.height) { |
---|
[13652] | 66 | best_size = {width:width,height:height}; |
---|
[12678] | 67 | } |
---|
| 68 | break; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | if (line <= 2) { |
---|
[12679] | 73 | if (max_height == 0 || line == 1) { |
---|
[12710] | 74 | height = GThumb.big_thumb.height; |
---|
[12679] | 75 | } else { |
---|
| 76 | height += max_height; |
---|
| 77 | } |
---|
[12710] | 78 | if (height >= best_size.height && width/height >= min_ratio && height<=GThumb.big_thumb.height) { |
---|
[12679] | 79 | best_size = {width:width,height:height} |
---|
| 80 | } |
---|
[12678] | 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
[12710] | 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; |
---|
[12678] | 88 | } |
---|
[12710] | 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); |
---|
[12678] | 91 | |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if (best_size.width == 1) { |
---|
[12710] | 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; |
---|
[12678] | 99 | } |
---|
[12710] | 100 | GThumb.t[0].crop = false; |
---|
[12678] | 101 | } |
---|
| 102 | |
---|
[12710] | 103 | width_count = GThumb.margin; |
---|
[12678] | 104 | max_height = 0; |
---|
| 105 | line = 1; |
---|
| 106 | thumb_process = new Array; |
---|
| 107 | |
---|
[12710] | 108 | for (i=GThumb.t[0].crop!=false?1:0;i<GThumb.t.length;i++) { |
---|
[12678] | 109 | |
---|
[12710] | 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]); |
---|
[12678] | 113 | |
---|
| 114 | available_width = main_width; |
---|
[12710] | 115 | if (line <= 2 && GThumb.t[0].crop !== false) { |
---|
| 116 | available_width -= (GThumb.t[0].crop + GThumb.margin); |
---|
[12678] | 117 | } |
---|
| 118 | |
---|
| 119 | if (width_count > available_width) { |
---|
| 120 | |
---|
[13652] | 121 | last_thumb = GThumb.t[i].index; |
---|
[12678] | 122 | ratio = width_count / available_width; |
---|
| 123 | new_height = Math.round(max_height / ratio); |
---|
| 124 | round_rest = 0; |
---|
[12710] | 125 | width_count = GThumb.margin; |
---|
[12678] | 126 | |
---|
| 127 | for (j=0;j<thumb_process.length;j++) { |
---|
| 128 | |
---|
[13652] | 129 | if (thumb_process[j].index == last_thumb) { |
---|
[12710] | 130 | new_width = available_width - width_count - GThumb.margin; |
---|
[12678] | 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 | } |
---|
[13652] | 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); |
---|
[12678] | 137 | |
---|
[12710] | 138 | width_count += new_width + GThumb.margin; |
---|
[12678] | 139 | } |
---|
| 140 | thumb_process = new Array; |
---|
[12710] | 141 | width_count = GThumb.margin; |
---|
[12678] | 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++) { |
---|
[13652] | 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); |
---|
[12678] | 150 | } |
---|
| 151 | |
---|
[12710] | 152 | if (main_width != jQuery('#thumbnails').width()) { |
---|
| 153 | GThumb.process(); |
---|
[12678] | 154 | } |
---|
| 155 | }, |
---|
| 156 | |
---|
[12691] | 157 | resize: function(thumb, width, height, new_width, new_height, is_big) { |
---|
[12678] | 158 | |
---|
[12710] | 159 | if (GThumb.method == 'resize' || height < new_height || width < new_width) { |
---|
[12696] | 160 | real_width = new_width; |
---|
| 161 | real_height = new_height; |
---|
[12691] | 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 | } |
---|
[12678] | 174 | thumb.css({ |
---|
[12691] | 175 | height: real_height+'px', |
---|
| 176 | width: real_width+'px' |
---|
[12678] | 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 | } |
---|