0

I'm trying to send encoded data for a bitmap image to a server and back to the( or a separate) client. There's probably an easier way to do this with sockets or something, but I'm a very casual and still a pretty new programmer and I had trouble understanding other methods. The encoding and decoding works fine and I can save and open up the encoded png, but somewhere on the php/mysql side the encoded data loses most of it's code.

Actionscript code for sending the encoded data to the php.

var req:URLRequest = new URLRequest("http://host.net//img.php");
req.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader;
var phpvar = new URLVariables();

var shape:MovieClip = new MovieClip();
shape.graphics.beginFill(0xFF0000);
shape.graphics.drawRect(0, 0, 100, 100);
addChild(shape);
var bmp:BitmapData = new BitmapData(500, 350);
bmp.draw(shape);

var byte:ByteArray = new ByteArray();
bmp.encode(new Rectangle(0,0, 100, 100), new flash.display.PNGEncoderOptions(false), byte);

//save on database
phpvar.bytez = byte;
req.data = phpvar;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(req);

trace(req.data.bytez);

The traced output is:

PNG

->

IHDRddÿIDATxÚíÐ1 µçYÁÏ"Ð)®F,Y²dɒ¥@,Y²dÉR K,Y²d)%K,Y²Ȓ%K,Y dɒ%K,²dɒ%KY²dɒ%K,Y²dɒ¥@,Y²dÉR K,Y²d)%K,Y²Ȓ%K,Y dɒ%K,²dɒ%KY²dɒ%K,Yß^Çjú··IEND®B`

Actionscript code for recieving data:

var req = new URLRequest("http://host.net//imgsend.php");
var loader = new URLLoader(req);


loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(req);



function onComplete(e:Event) {
    var loaderer:Loader = new Loader();
    var byte:ByteArray = new ByteArray()

    byte = e.target.data;
    trace(byte);
}

Traced output is:

PNG

->

For some reason it knows the encoded data is for a PNG, but all there is is a header. I think the problem is in my php/mysql code, but I'm not completely sure here. I'm using a free webhosting service with phpMyAdmin for convenient sake, the Images table is just a BLOB column and the php files are uploaded on the host server.

PHP code for reading and inserting into mysql (img.php)

<?php
mysql_connect($mysql_host, $mysql_user, $mysql_password);

mysql_select_db($mysql_database) or die(mysql_error());


$pic = $_POST['bytez'];


$sql = "INSERT INTO Images (img) VALUES ('$pic')";

mysql_query($sql);
?>

PHP code for taking the data form the server and sending it to Actionscript (imgsend.php)

<?php
mysql_connect($mysql_host, $mysql_user, $mysql_password);

mysql_select_db($mysql_database) or die(mysql_error());




$sql = "SELECT * FROM Images";

$records = mysql_query($sql);

while($byte=mysql_fetch_assoc($records)){

    echo $byte['img'];

}

?>
7
  • Just set a proper headers in PHP so that the output IS image, and then use Loader inside ActionScript in order to load image. This way you won't do the encoding/decoding by yourself, it will be automatic. May 26, 2015 at 15:16
  • 1
    You are vulnerable to sql injection attacks.
    – Marc B
    May 26, 2015 at 15:17
  • even encoded what you send to the server is a string so save it as string (TEXT on sqlite)
    – BotMaster
    May 26, 2015 at 15:22
  • I know a little of that Marc B, thanks! This isn't for any final product yet, mostly testing. If I do end up turning this into a public project I will definitely look more into that. I'm still lost for storing the image as an image inside mysql, but I'll try messing with TEXT fields on sql.
    – Nikku
    May 26, 2015 at 16:25
  • Web isn't my strong suite, but I believe you store the image on the file system, and the URL of the image in MySQL. I don't think you're supposed to be able to store the image itself in SQL. So you have the database return the URL, and have Flash download the image from the URL with another Loader.
    – user45623
    May 27, 2015 at 0:45

1 Answer 1

0

I didn't necessarily find an answer to this exact method, but I found an alternate solution, ie writing the file directly into the directory. From what I've read this is much more efficient anyways. The code is basically the same only I added URLcontentType and extended the URL for the URL request address for the file's location.

var req:URLRequest = new URLRequest("http://host.net//img.php?filename=something.png");
req.method = URLRequestMethod.POST;
req.contentType = "application/octet-stream";

var loader:URLLoader = new URLLoader;


var shape:MovieClip = new MovieClip();
shape.graphics.beginFill(0xFF0000);
shape.graphics.drawRect(0, 0, 100, 100);
addChild(shape);
var bmp:BitmapData = new BitmapData(500, 350);
bmp.draw(shape);

var byte:ByteArray = new ByteArray();
bmp.encode(new Rectangle(0,0, 100, 100), new flash.display.PNGEncoderOptions(false), byte);

//save on database
req.data = byte;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(req);

The PHP code is for img.php is:

<?php

   $fileName = $_GET['filename'];

   $fp = fopen( $fileName, 'wb' );

   fwrite( $fp, $GLOBALS['HTTP_RAW_POST_DATA'] );    

   fclose( $fp );

?>

Loading the file is simple, not going to bother posting that. I'll deal with unique file names on my own, baby steps. Thanks for helping me talk this out some!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.