mardi 26 mai 2015

Sending and Recieving PNGencoded to php mysql BLOB

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("");
req.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader;
var phpvar = new URLVariables();

var shape:MovieClip = new MovieClip();;, 0, 100, 100);
var bmp:BitmapData = new BitmapData(500, 350);

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; = phpvar;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;


The traced output is:



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("");
var loader = new URLLoader(req);

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

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

    byte =;

Traced output is:



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 myphpadmin 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)

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')";


PHP code for taking the data form the server and sending it to Actionscript (imgsend.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);


    echo $byte['img'];



I know the php code is sloppy at best, and I'll deal with processing what gets fetched by the query when the time comes. Any help would be great, even if you just help me clean up the sloppy code lol.

Aucun commentaire:

Enregistrer un commentaire