Search code examples
phpimageactionscript-3blobaddchild

How to get blob image from mysql to as3 with php and addChild it in a way of getting byteArray and converting it to image?


How to get blob image from mysql to as3 with php and addChild it in a way of getting byteArray and converting it to image?

this is php:

if($RequestType == 'Select'){

  while($row=mysql_fetch_assoc($results)){
      $arrdata[]=$row;
  }

  foreach($arrdata as $key=>$value){
    $output[$key] =  $arrdata[$key];
    $output[$key]['png'] = base64_encode($arrdata[$key]['png']);
  }

header('Content-type: application/json');
print(json_encode($output));

i've already got something like this, but longer:

VBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAC4jAAAuIwF4pT92AAAYxUlEQVR42u1bB3Sc1ZWe8k/vvfem6aPpGk2RZjQzGrVRl6xiyZZkWS4gFxyDGyEsxjE1gVBTSGAdQg4keyCxF4NsY1pCDARsBwiE4pBsIJjN7nKWgM3eJ7/hTDhUIyJyNj7nHh3P/DP/f79373e/e98b0oUXXkj6/2ykfwLwTwAW58br1q0j9fX1UQcGBpgjw0Pcy


Solution

  • (In this I'm assuming you have already handled the HTTP request result)

    To transform a base64 encoded image to a DisplayObject, you first need to decode the Base64 to a ByteArray then use the Loader class to load the image content, listening for the Loader's Event.COMPLETE event to dispatch. Once you've got that event Loader.content will contain a DisplayObject that you can add to the screen using addChild().

    Example: (EDIT: Added an Array to keep track of loaded objects in order they were requested and altered function to calls to cope)

    private var countLoadedImages:int = 0;//track how many images have loaded
    private var arrImageLoaders:Array = new Array();//store loaders for call back
        public function loadImageFromBase64(yourBase64:String):void
        {
            var base64:Base64Decoder = new Base64Decoder();
            base64.decode(yourBase64);//decode your image
    
            var data:ByteArray = base64.toByteArray();//convert to byte array
    
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
            arrImageLoaders.push(loader.contentLoaderInfo);//add to array in order func was called
            loader.loadBytes(data);//hand over to Loader
        }
        protected function onLoadComplete(event:Event):void
        {
            countLoadedImages++;
            event.target.removeEventListener(Event.COMPLETE,onLoadComplete);
            if (countLoadedImages == arrImageLoaders.length)
            {
                 allImagesLoaded();
            }
        }
        protected function allImagesLoaded():void
        {
            var contentLoaderInfo:LoaderInfo
            for (var x:int = 0; x<arrImageLoaders.length; x++)
            {
                contentLoaderInfo = arrImageLoaders[x] as LoaderInfo;
                addChild(contentLoaderInfo.content);
            }
        }