如何将带有图像的字节数组从 AS3 发送到 PHP?

How to send byte array with image from AS3 to PHP?

我从 AS3 AIR App 发送图像,除了 POST 参数的其余部分到 PHP 脚本,它将完成其余部分。我想以某种方式将带有图像的字节数组转换为字符串并使用 base64 对其进行编码。成功了,但是图片数据有误

这是我用来转换它的代码:

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

一切正常,此数据已发送到服务器,已解码并存储为图像,但图像错误。不知何故它大约是 40 kb,而当我将它保存在 Air 应用程序中时它是 22 kb。有什么想法吗?

p.s。我知道我可以将它保存在本地并上传,但我真的需要这样做。此外,BA1.readUTF() 会产生错误,因此不是一个选项。

加法:

在服务器端,我在写入文件之前尝试 utf8_decode 字符串,不知何故我得到了一个尺寸合适的图像,但是......那个图像不是我想要的,它看起来像涂鸦...

找到灵魂。我已经从 http://www.sociodox.com/base64.html Base64.swc 下载了,它实际上是编码和解码图像字节数组。因为我的字符串是 JSON-ed(作为发送到 PHP 的对象的一部分),我只需要将空格转换为“+”并将其解码并写入文件 - 完美无缺!案件结案。

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

当然,也设置你的听众...

在"some.php"中:

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

这很有魅力:)