0

I have from AS3 AIR App to send an image, beside rest of POST parameters to the PHP script which will do the rest. I want to somehow convert byte array with image to string and encode it with base64. I was successfull, but image data is wrong.

Here is the code I used to convert it:

...
//BA1 is Byte Array with an image in it
var data:String = BA1.toString();
OutSql.push({t: "b1", v: Base64.encode(data)});
...

Everything works fine, this data is sent to server, decoded, and stored as an image, but image is wrong. Somehow it is about 40 kb, while when I save it within Air application it is 22 kb. Any ideas?

p.s. I know that I can save it localy and upload it, but I really need to do it this way. Also, BA1.readUTF() generates an error, so not an option.

Addition:

On a server side I have tried to utf8_decode string before writing to file, and somehow I got an image which is proper dimensions, but... that image is not what I wanted to be, it looks like scribble...

4
  • 1
    Does BA1 contain data of jpeg or png or just raw bitmapData (used getPixels(); for bytes)? If its already jpg/png then use Base64.encode(BA1) then the B64 itself will give you an encoded string from the bytes. Anyway your size is 40kb (from 22kb) cos you used toString. Consider two bytes 0xFF and 0xD8 (total size is 2) now as a string it is FFD8 which is total size 4. The conversion is doubling the size. If possible just do a trace ("bytes are : " + BA1); then show me here the first 8 letters of the BA1 bytes. Easier to assess the format that way... Commented Apr 22, 2016 at 15:50
  • BA1 contains jpeg data. However, trying to encode BA1 generates an error, cause only string can be encoded, not a byte array. Commented Apr 22, 2016 at 16:24
  • 1
    Here is answer for trace: bytes are : ÿØÿàJFIFÿÛ which looks like a good jpeg header. But the rest of data is just messed up. Image siye is correct, by the way. Commented Apr 22, 2016 at 16:28
  • Why not send the image as binary? Commented Apr 22, 2016 at 16:31

2 Answers 2

3
import com.sociodox.utils.Base64;
.....
//BA1 is ByteArray with an image encoded
var enc_image=Base64.encode(BA1);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
var request:URLRequest = new URLRequest("some.php");
var variables:URLVariables = new URLVariables();
variables.decode("image="+enc_image);
request.method = URLRequestMethod.POST;
request.data = variables;
loader.load(request);

of course, set your listeners, too...

in "some.php":

$imageData = base64_decode(str_replace(" ", "+", $_POST['image']));
$fh = fopen("path/to/image/somename.jpg", "wb");
fwrite($fh, $imageData);
fclose($fh);

This works like a charm :)

Sign up to request clarification or add additional context in comments.

Comments

1

Soultion found. I have downloaded from http://www.sociodox.com/base64.html Base64.swc which is actually encoding and decoding image Byte Arrays. And because my string was JSON-ed (as a part of object sent to PHP) I had only to convert spaces to '+' and decode it and write to file - works perfect! Case closed.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.