0

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.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
willa_012
  • 46
  • 6
  • 1
    A way too broad....Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.... – B001ᛦ Sep 28 '17 at 09:40
  • Share what you've tried so far, it can be a good starting point for someone to answer. You can get the pixel data of a movie clip and encode it to a PNG then send that image data over to PHP for saving in a database. – BadFeelingAboutThis Sep 28 '17 at 16:48
  • The same as the link, but use **PNGEncoder** instead of **JPGEncoder**: https://stackoverflow.com/a/9405622/4687633 Found it by **Google > as3 screenshot png php** – Organis Sep 28 '17 at 19:48
  • I've updated my question, added the script that what I'm trying to make it work. – willa_012 Sep 29 '17 at 07:03
  • Try `if(isset($GLOBALS["HTTP_RAW_POST_DATA"])) { echo $GLOBALS['HTTP_RAW_POST_DATA'] }` what does the echo say? If many lines then show beginning 1 or 2 lines. This is just to be sure that PHP received the PNG data. – VC.One Sep 29 '17 at 11:09
  • PS: Also make sure `new URLRequest("saveScene.php");` is pointing to a PHP-enabled `http://` server not just a text file. Something like `new URLRequest("http://localhost/saveScene.php");` or similar access to your server + PHP file. – VC.One Sep 29 '17 at 11:13
  • @willa_012 What made it work? Please provide your solution in **Answer** box if you think it will help others. – VC.One Sep 29 '17 at 15:52
  • I'm Sorry I made a mistake. I thought it was already working. I've used if(isset($GLOBALS["HTTP_RAW_POST_DATA"])) { echo $GLOBALS['HTTP_RAW_POST_DATA'] } but when I refresh the php page after saving the movieclip into png, it displays nothing. – willa_012 Sep 29 '17 at 16:40
  • How would I save the transferred png data to mysql in the php? – willa_012 Sep 29 '17 at 16:48
  • @willa_012 I'm no mySQL expert. I just use PHP to access or modify (media) file bytes. Any saving I do is to disk not database. I'll test your code when I get a chance. It's 6pm here, do not expect answer until morning. – VC.One Sep 29 '17 at 17:27
  • _"How would I save the transferred png data to mysql in the php?"_ Why not just save PNG to your server as file in some folder? Then your database just holds page _names_ and their _links_, which you will later query as needed. Read [**this advice**](https://stackoverflow.com/questions/6472233/can-i-store-images-in-mysql) also. – VC.One Oct 04 '17 at 07:55
  • Saving page names in database is a good idea, but we don't know how to do it with as3. We have the plan that page images can be retrieve from mysql to UI Loader of Flash. – willa_012 Oct 04 '17 at 11:12

0 Answers0