28
28
package util {
29
29
import by.blooddy.crypto.serialization.JSON ;
30
30
31
+ import flash.display.BitmapData ;
31
32
import flash.display.Loader ;
32
33
import flash.events.ErrorEvent ;
33
34
import flash.events.Event ;
34
35
import flash.events.HTTPStatusEvent ;
35
36
import flash.events.IOErrorEvent ;
36
37
import flash.events.SecurityErrorEvent ;
38
+ import flash.geom.Matrix ;
37
39
import flash.net.SharedObject ;
38
40
import flash.net.URLLoader ;
39
41
import flash.net.URLLoaderDataFormat ;
@@ -94,7 +96,7 @@ public class Server implements IServer {
94
96
// Re-trying here should help project save failures but we'll need to add more code to re-try loading projects
95
97
if (event is SecurityErrorEvent ) {
96
98
var urlPathStart: int = url . indexOf ('/' , 10 );
97
- var policyFileURL: String = url . substr (0 , urlPathStart) + '/crossdomain.xml?cb=' + Math . random ();
99
+ var policyFileURL: String = url . substr (0 , urlPathStart) + '/crossdomain.xml?cb=' + Math . random ();
98
100
Security . loadPolicyFile (policyFileURL);
99
101
Scratch. app. log ('Reloading policy file from : ' + policyFileURL);
100
102
}
@@ -142,6 +144,7 @@ public class Server implements IServer {
142
144
}
143
145
144
146
var httpStatus: int = 0 ;
147
+
145
148
function errorHandler(event: ErrorEvent ): void {
146
149
removeListeners();
147
150
onCallServerError(url , data , event);
@@ -231,7 +234,7 @@ public class Server implements IServer {
231
234
return serverGet(url , whenDone);
232
235
}
233
236
234
- public function getThumbnail ( idAndExt :String , w :int , h :int , whenDone :Function ):URLLoader {
237
+ protected function downloadThumbnail ( url :String , w :int , h :int , whenDone :Function ):URLLoader {
235
238
function decodeImage(data : ByteArray ): void {
236
239
if (! data || data . length == 0 ) return ; // no data
237
240
var decoder: Loader = new Loader ();
@@ -244,23 +247,40 @@ public class Server implements IServer {
244
247
Scratch. app. logException(e);
245
248
}
246
249
else {
247
- Scratch. app. logMessage('Server caught exception decoding image: ' + idAndExt );
250
+ Scratch. app. logMessage('Server caught exception decoding image: ' + url );
248
251
}
249
252
}
250
253
}
251
254
252
255
function imageError(e: IOErrorEvent ): void {
253
- Scratch. app. log ('ServerOnline failed to decode image: ' + idAndExt );
256
+ Scratch. app. log ('ServerOnline failed to decode image: ' + url );
254
257
}
255
258
256
259
function imageDecoded(e: Event ): void {
257
- whenDone(e. target . content . bitmapData );
260
+ whenDone(makeThumbnail( e. target . content . bitmapData ) );
258
261
}
259
262
260
- var url : String = getCdnStaticSiteURL() + 'medialibrarythumbnails/' + idAndExt;
261
263
return serverGet(url , decodeImage);
262
264
}
263
265
266
+ private static function makeThumbnail (bm :BitmapData ):BitmapData {
267
+ const tnWidth: int = 120 ;
268
+ const tnHeight: int = 90 ;
269
+ var result : BitmapData = new BitmapData (tnWidth, tnHeight, true , 0 );
270
+ if ((bm. width == 0 ) || (bm. height == 0 )) return result ;
271
+ var scale : Number = Math . min (tnWidth / bm. width , tnHeight / bm. height );
272
+ var m: Matrix = new Matrix ();
273
+ m. scale (scale , scale );
274
+ m. translate ((tnWidth - (scale * bm. width )) / 2 , (tnHeight - (scale * bm. height )) / 2 );
275
+ result . draw (bm, m);
276
+ return result ;
277
+ }
278
+
279
+ public function getThumbnail (idAndExt :String , w :int , h :int , whenDone :Function ):URLLoader {
280
+ var url : String = getCdnStaticSiteURL() + 'medialibrarythumbnails/' + idAndExt;
281
+ return downloadThumbnail(url , w, h, whenDone);
282
+ }
283
+
264
284
// -----------------------------
265
285
// Translation Support
266
286
//------------------------------
0 commit comments