In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2011-10-21

@Author: kimo0517
Convert any image to Base64 String. 
For adding image in code easily. 
example of decoding: http://wonderfl.net/c/wKps
Get Adobe Flash player
by zeg.email 21 Oct 2011
    Embed
/**
 * Copyright zeg.email ( http://wonderfl.net/user/zeg.email )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jPby
 */

/*
 * @Author: kimo0517
 * Convert any image to Base64 String. 
 * For adding image in code easily. 
 * example of decoding: http://wonderfl.net/c/wKps
 */

package 
{
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.desktop.Clipboard;
    import flash.desktop.ClipboardFormats;
    import com.bit101.components.PushButton;
    import flash.external.ExternalInterface;
    
    import flash.net.FileReference;
    import flash.net.FileFilter;
    
    import mx.utils.Base64Encoder;    
    
    [SWF(width=200, height=100, backgroundColor=0xAADDFF)]
    public class Image2Base64 extends Sprite 
    {
        private var base64Str:String = "";
        private var bmd:Bitmap;
        private var btnSelectPic:PushButton;
        private var fr:FileReference;
        
        private var base64Encoder:Base64Encoder;
        
        public function Image2Base64():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            btnSelectPic = new PushButton(this, 5, 5, "Select Photo", _btnSelectPicClicked);
         
            
            bmd = new Bitmap;
            bmd.y = btnSelectPic.y + btnSelectPic.height + 5;
            bmd.smoothing = true;
            addChild(bmd);
        }
        

        
        private function _btnSelectPicClicked(e:MouseEvent):void {
            fr = new FileReference();
            fr.addEventListener(Event.SELECT, _startLoadPhoto);
            fr.addEventListener(Event.COMPLETE, _loadPhotoComplete);
            
            var filter:FileFilter = new FileFilter("Images (*.jpg, *.png, *.gif)", "*.jpg;*.jpeg;*.png;*.gif", null); 
            
            fr.browse([filter]); 
            
        }
        
        private function _startLoadPhoto(e:Event):void {
            fr.load();
        }
        
        private function _loadPhotoComplete(e:Event):void {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _loadBMDComplete);
            loader.loadBytes(fr.data);
            
            base64Encoder = new Base64Encoder;
            base64Encoder.insertNewLines=false
            base64Encoder.encodeBytes(fr.data);
            base64Str = "data:image/"+fr.type.substr(1)+";base64,"+base64Encoder.toString();
            if(!root.loaderInfo.parameters.callback)
                ExternalInterface.call('alert','missing parameter : callback')
            ExternalInterface.call( root.loaderInfo.parameters.callback,base64Str)    
        }
        
        private function _loadBMDComplete(e:Event):void {  
            if (bmd.bitmapData != null) {
                bmd.bitmapData.dispose();
            }  
            bmd.bitmapData = e.target.content.bitmapData.clone();
            bmd.height = 100 - bmd.y;
            bmd.width = 200;  
            if (bmd.scaleX < bmd.scaleY) {
                bmd.scaleY = bmd.scaleX;
            } else {
                bmd.scaleX = bmd.scaleY;
            }
            
        }
        
    }
    
}