as3 - PNGEncoder - save to server

Hi

I am struggling to encode and then save a png image onto the server. The file ‘myImage.png’ gets saved onto the server but it is 0kb. Can anyone help me with this?

My actionscript is:


import com.adobe.images.PNGEncoder;
import flash.display.BitmapData;
import flash.utils.ByteArray;

// Go to PHP Page.//
bitmap_btn.addEventListener(MouseEvent.CLICK, gotoPHP);

function gotoPHP(event:Event):void {
	SavePNGExample();
	var reqVariables:URLRequest = new URLRequest ("xxxxxxxxxxx/63KDJD99D/saveImage.php");
	navigateToURL(reqVariables, "_self");
}
//

// Save PNG.//
function SavePNGExample():void {
	var bmpData:BitmapData = new BitmapData(image_mc.width, image_mc.height, false);
	bmpData.draw(image_mc);
	var imageByteArray:ByteArray = PNGEncoder.encode(bmpData);        
	var req:URLRequest = new URLRequest("xxxxxxxxxxx/63KDJD99D/saveImage.php");
	req.contentType = "application/octet-stream";
	req.method = URLRequestMethod.POST;
	req.data = imageByteArray;  
}
//

and the php is:


<?php
   $fp = fopen('myImage.png', 'wb');
   fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
   fclose($fp);
?>

Can anyone help?

Check with an http monitor (e.g charles web debugging proxy) that valid data is getting transferred to the server. If it is, then check the file permissions of where you are trying to write the image file.

That and try the JPGEncoder class. I was having problems with the PNGEncoder because I didn’t exactly know how to use it.

I found some sample JPGEncoder class code and that got my app working.