{"id":145,"date":"2010-09-05T16:28:21","date_gmt":"2010-09-05T15:28:21","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=145"},"modified":"2013-09-18T14:14:17","modified_gmt":"2013-09-18T13:14:17","slug":"resize-an-image-and-make-a-thumb","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/resize-an-image-and-make-a-thumb\/","title":{"rendered":"Resize an image and make a thumb !"},"content":{"rendered":"<p>Hey !<br \/>\nAfter two months without any article, I&#8217;m ready to start again activity on this blog ! For sure, it&#8217;s the start of the new school year !<\/p>\n<p>So today, a little script really useful to make a thumb in ActionScript 3 !<br \/>\nEDIT : deleted my previous script, this one coming from <a href=\"http:\/\/www.cultcreative.com\/tutorials\/11\/18\/2011\/working-with-bitmaps-and-bitmapdata-in-as3\/\" target=\"_blank\">Cult Creative<\/a> is awesome :<\/p>\n<pre lang=\"actionscript3\">\/**\r\n* fitImage\r\n* @ARG_object   the display object to work with\r\n* @ARG_width    width of the box to fit the image into\r\n* @ARG_height   height of the box to fit the image into\r\n* @ARG_center   should it offset to center the result in the box\r\n* @ARG_fillBox  should it fill the box, may crop the image (true), or fit the whole image within the bounds (false)\r\n**\/\r\n\r\nprivate function fitImageProportionally( ARG_object:DisplayObject, ARG_width:Number, ARG_height:Number, ARG_center:Boolean = true, ARG_fillBox:Boolean = true ):Bitmap {\r\n \r\n    var tempW:Number = ARG_object.width;\r\n    var tempH:Number = ARG_object.height;\r\n \r\n    ARG_object.width = ARG_width;\r\n    ARG_object.height = ARG_height;\r\n \r\n    var scale:Number = (ARG_fillBox) ? Math.max(ARG_object.scaleX, ARG_object.scaleY) : Math.min(ARG_object.scaleX, ARG_object.scaleY);\r\n \r\n    ARG_object.width = tempW;\r\n    ARG_object.height = tempH;\r\n \r\n    var scaleBmpd:BitmapData = new BitmapData(ARG_object.width * scale, ARG_object.height * scale);\r\n    var scaledBitmap:Bitmap = new Bitmap(scaleBmpd, PixelSnapping.ALWAYS, true);\r\n    var scaleMatrix:Matrix = new Matrix();\r\n    scaleMatrix.scale(scale, scale);\r\n    scaleBmpd.draw( ARG_object, scaleMatrix );\r\n \r\n    if (scaledBitmap.width > ARG_width || scaledBitmap.height > ARG_height) {\r\n \r\n        var cropMatrix:Matrix = new Matrix();\r\n        var cropArea:Rectangle = new Rectangle(0, 0, ARG_width, ARG_height);\r\n \r\n        var croppedBmpd:BitmapData = new BitmapData(ARG_width, ARG_height);\r\n        var croppedBitmap:Bitmap = new Bitmap(croppedBmpd, PixelSnapping.ALWAYS, true);\r\n \r\n        if (ARG_center) {\r\n            var offsetX:Number = Math.abs((ARG_width -scaleBmpd.width) \/ 2);\r\n            var offsetY:Number = Math.abs((ARG_height - scaleBmpd.height) \/ 2);\r\n \r\n            cropMatrix.translate(-offsetX, -offsetY);\r\n        }\r\n \r\n        croppedBmpd.draw( scaledBitmap, cropMatrix, null, null, cropArea, true );\r\n        return croppedBitmap;\r\n \r\n    } else {\r\n        return scaledBitmap;\r\n    }\r\n \r\n}<\/pre>\n<p>An other method coming from Thibault Imbert !<\/p>\n<pre lang=\"actionscript3\">package {\r\n\r\n\timport flash.display.BitmapData;\r\n\timport flash.geom.ColorTransform;\r\n\timport flash.geom.Matrix;\r\n\r\n\t\/**\r\n\t * @author Maxime Cousinou\r\n\t *\/\r\n\tpublic class BitmapManager {\r\n\r\n\t\tpublic static function reduceBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {\r\n\r\n\t\t\tvar bmpData:BitmapData = new BitmapData(Math.round(bmp.width * ratio), Math.round(bmp.height * ratio), transparent, 0x00FFFFFF);\r\n\t\t\tvar scaleMatrix:Matrix = new Matrix(bmpData.width \/ bmp.width, 0, 0, bmpData.height \/ bmp.height, 0, 0);\r\n\t\t\tbmpData.draw(bmp, scaleMatrix);\r\n\r\n\t\t\treturn bmpData;\r\n\t\t}\r\n\r\n\t\tpublic static function resizeBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {\r\n\r\n\t\t\tvar bmpData:BitmapData = new BitmapData(Math.round(bmp.width * ratio), Math.round(bmp.height * ratio), transparent, 0x00FFFFFF);\r\n\t\t\tvar scaleMatrix:Matrix = new Matrix(bmpData.width \/ bmp.width, 0, 0, bmpData.height \/ bmp.height, 0, 0);\r\n\t\t\tvar colorTransform:ColorTransform = new ColorTransform();\r\n\t\t\tbmpData.draw(bmp, scaleMatrix, colorTransform, null, null, true);\r\n\r\n\t\t\treturn bmpData;\r\n\t\t}\r\n\r\n\t\tpublic static function resampleBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {\r\n\r\n\t\t\tif (ratio >= 1) {\r\n\t\t\t\treturn BitmapManager.resizeBitmapData(bmp, ratio, transparent);\r\n\t\t\t} else {\r\n\t\t\t\tvar bmpData:BitmapData = bmp.clone();\r\n\t\t\t\tvar appliedRatio:Number = 1;\r\n\r\n\t\t\t\tdo {\r\n\t\t\t\t\tif (ratio < 0.5 * appliedRatio) {\r\n\t\t\t\t\t\tbmpData = BitmapManager.resizeBitmapData(bmpData, 0.5, transparent);\r\n\t\t\t\t\t\tappliedRatio = 0.5 * appliedRatio;\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\tbmpData = BitmapManager.resizeBitmapData(bmpData, ratio \/ appliedRatio, transparent);\r\n\t\t\t\t\t\tappliedRatio = ratio;\r\n\t\t\t\t\t}\r\n\t\t\t\t} while (appliedRatio != ratio);\r\n\r\n\t\t\t\treturn bmpData;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hey ! After two months without any article, I&#8217;m ready to start again activity on this blog ! For sure, it&#8217;s the start of the new school year ! So today, a little script really useful to make a thumb in ActionScript 3 ! EDIT : deleted my previous script, this one coming from Cult &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/resize-an-image-and-make-a-thumb\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Resize an image and make a thumb !<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4,33],"tags":[15,95,96,124,34,123,125,122],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/145"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":13,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":953,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/145\/revisions\/953"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}