High quality, high performance thumbnails


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private static const SCALE_DROP:Number = .5;
  2.  
  3. public function resample(bitmapData:BitmapData, newWidth:uint, newHeight:uint):BitmapData
  4. {
  5. var finalScale:Number = Math.min(newWidth/bitmapData.width, newHeight/bitmapData.height);
  6. var finalData:BitmapData = bitmapData;
  7.  
  8. if (finalScale> 1) {
  9. finalData = new BitmapData(bitmapData.width*finalScale, bitmapData.height*finalScale, true, 0);
  10. finalData.draw(bitmapData, new Matrix(finalScale, 0, 0, finalScale), null, null, null, true);
  11. return finalData;
  12. }
  13.  
  14. var drop:Number = SCALE_DROP;
  15. var initialScale:Number = finalScale;
  16. while (initialScale/drop <1) initialScale /= drop;
  17.  
  18. var bd:BitmapData = new BitmapData(Math.ceil(bitmapData.width*initialScale), Math.ceil(bitmapData.height*initialScale));
  19. bd.draw(finalData, new Matrix(initialScale, 0, 0, initialScale), null, null, null, true);
  20. finalData = bd;
  21.  
  22. for (var scale:Number = initialScale*drop; Math.round(scale*1000)>= Math.round(finalScale*1000); scale *= drop) {
  23. bd = new BitmapData(Math.ceil(bitmapData.width*scale), Math.ceil(bitmapData.height*scale));
  24. bd.draw(finalData, new Matrix(drop, 0, 0, drop), null, null, null, true);
  25. finalData = bd;
  26. }
  27.  
  28. return finalData;
  29. }

URL: http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.