FalsAlarm Posted February 7, 2014 Share Posted February 7, 2014 Hello I have a flash actionscript 3 project that creates a PNG image out of any Flash MovieClip. inside my as3 code i have this code which creates a successful POST to my php script. My question is how do I take the byte array variable and write it to the filesystem as a PNG image. //prepare data to send to php var bitmapData:BitmapData = graffitiGalore.drawingTab.main.canvas.drawing(true); var byteArray:ByteArray = PNGEncoder.encode(bitmapData); //create a parameters object var parametersObject:Object = new Object(); //create a POST variable to be sent to php parametersObject.byte_array = byteArray; //create a new PHPRequest and pass in the parametersObject var phpRequest:PHPRequest = new PHPRequest(); phpRequest.makeRequest(ApplicationConstants.SAVE_IMAGE, parametersObject); phpRequest.addEventListener(Event.COMPLETE, onCompleteSaveImage); ); Ok, so in php i'll have access to $_POST['byte_array'] variable which contains the PNG image byte array as data. How do I take this data and write a real PNG image to the filesystem? I'm new to lots of php stuff still. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 7, 2014 Share Posted February 7, 2014 file_put_contents Depending on your system you may need to give PHP the permissions to create files in whatever folder: with *nix you can chmod 0777 (not optimal but gets you working quickly) and on Windows it... depends. on things. Quote Link to comment Share on other sites More sharing options...
FalsAlarm Posted February 7, 2014 Author Share Posted February 7, 2014 file_put_contents Depending on your system you may need to give PHP the permissions to create files in whatever folder: with *nix you can chmod 0777 <folder> (not optimal but gets you working quickly) and on Windows it... depends. on things. Alright, I tried your idea. Below is a PHP object that I wrote to handle the SaveImage feature of my Flash application. <code> <?php error_reporting(E_ALL); ini_set('display_errors','On'); $newUser = new SaveImage(); class SaveImage { //a reference to thd facebook object public $facebook; //holds the app users uid public $id; //holds the selected users uid public $targetUID; //the byte array data public $byteArray; //holds a reference to the db connection public $connection; //the output that gets returned to flash public $outputObject; public function __construct() { //include the database connection include 'database.php'; //include the php sdk //include the facebook sdk library require_once('facebook.php'); //instantiate a new Facebook Object, you must replace your APP_ID with your real apps app id. do the same for the secret. $this->facebook = new Facebook(array( 'appId' => 'removed', 'secret' => 'removed', 'cookie' => true)); //store a reference to the db connection in this object $this->connection = $connection; //initialize the outputObject $this->outputObject = new stdClass(); //attempt to get the current user $this->id = mysqli_real_escape_string($this->connection, $this->facebook->getUser()); $this->byteArray = $this->getByteArray(); $this->targetUID = $this->getTargetUID(); if($this->targetUID != NULL) { //continue if(isset($this->byteArray)) { //create a filepatch where to put the file. $filename = "wallImages/" . $this->targetUID . "_wallImage.png"; //use file put contents to put the file $result = file_put_contents($filename, $this->byteArray); $this->outputObject->result = "Sucess!"; } } echo json_encode($this->outputObject); } public function getByteArray() { return $_POST['byte_array']; } public function getTargetUID() { if(strlen($_POST['selectedWall']) > 0) { return mysqli_real_escape_string($this->connection, $_POST['selectedWall']); } else { return NULL; } } } ?> </code> It's kind of odd, but i'm getting the final output PNG file created, but it's only 1kb in size and doesn't open up in any image editors. It says the data might be corrupted when I try to open it. Quote Link to comment Share on other sites More sharing options...
FalsAlarm Posted February 7, 2014 Author Share Posted February 7, 2014 Doesn't PHP have any PNG functions for working with bitmap data or anything like that? Quote Link to comment Share on other sites More sharing options...
.josh Posted February 7, 2014 Share Posted February 7, 2014 Yes. Look into using the GD Library. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 7, 2014 Share Posted February 7, 2014 More specifically, imagecreatefromstring and imagepng Something along the lines of this: <?php $image = imagecreatefromstring($_POST['byte_array']); imagepng($image,'filename.png'); ?> Though FYI, simply using file_put_contents should have worked. If it didn't work, that means your data isn't right. Perhaps it needs to be base64_encoded or decoded? IOW I suspect you may run into issues even using these GD functions, because file_put_contents should have worked. But anyways, using the GD functions is safer since it offers a level of validation that it's actually real image data, before writing to your server.. Quote Link to comment Share on other sites More sharing options...
Solution FalsAlarm Posted February 8, 2014 Author Solution Share Posted February 8, 2014 Issue Resolved. The problem is that since I was POSTING the data to php, i needed to Base64 encode the byte array before hand inside my Actionscript 3. Then on the receiving end php file, I needed to Base64_decode($byte_array) which works perfectly, the Drawing is sent from my flash GUI to the server backend and it works!!! thanks to everyone that posted Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.