I'm creating an editor using AS3 where user can design a page by clicking some images that will display it's duplicate on that page. So that page is a movieclip, which is page_mc. I want to save the page_mc to mysql database (img colum on tbl_pages) as PNG image.
I'm aware that Php is use in order to connect the ActionScript3 to MySql. The problem is that I'm a beginner in AS3 and Php, I don't know how to link the image to php from as3, and what variable in the php part should I save on database. I have tried looking for sample script but I can't find the exact sample that I'm looking for. But this what I've tried so far:
in AS3:
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import flash.net.URLRequestHeader;
import flash.net.URLLoader;
import flash.events.Event;
import flash.geom.Matrix;
import com.adobe.images.PNGEncoder;
btn_save.addEventListener(MouseEvent.CLICK, savePage);
function savePage(event:MouseEvent):void
{
var pngEncoder:PNGEncoder;
pngEncoder = new PNGEncoder();
var bitmap:BitmapData = new BitmapData(page_mc.width, page_mc.height);
bitmap.draw(page_mc, new Matrix());
var img:ByteArray = PNGEncoder.encode(bitmap);
var sendHeader:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var sendReq:URLRequest = new URLRequest("saveScene.php");
sendReq.requestHeaders.push(sendHeader);
sendReq.method = URLRequestMethod.POST;
sendReq.data = img;
}
Php:
<?php
define("HOST", "localhost");
define("USERNAME", "root");
define("PASSWORD", "");
define("DB", "scenezone");
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DB);
if (!$con) {
die("Database Connection Failed!");
}
else
{
if(isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
$filename = "filename.png";
$fp = fopen($filename, "wb");
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
echo "filename =".$filename."&base=".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
}
}
?>
Please let me know what should I add if I have mistakes or missing functions on the script.