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


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

我从AS3 AIR应用程序发送图像,其余的POST参数到PHP脚本,剩下的就是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。有什么想法吗?

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

另外

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

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

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

在"一些.php"中:

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

这就像一个魅力:)

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