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("http://ift.tt/1HIqazg");
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://ift.tt/1LDeFHr");
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 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)

<?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'];

}

?>

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