Flash 可以通过各种途径获取或生成图片的 ByteArray 数据, 尤其是 Flash Player 10 增加了 FileReference.load 方法之后, 更是方便了许多, 最典型的一个应用场景就是用 Flash 编辑图片.
在 player 10 以前, 通常的做法是: 打开图片 -> 上传 -> 返回图片地址 -> 加载 -> 处理 -> 再上传.
player 10 以后就方便了, 直接用 load 方法打开本地图片, 用 Loader.loadBytes 方法显示图片就完成了上面说的前四步.
最终上传也很简单, AS 代码如下:
- var uper:URLLoader = new URLLoader();
- var ur:URLRequest = new URLRequest(UP_URL);
- ur.contentType = 'application/octet-stream';
- ur.method = URLRequestMethod.POST;
- ur.data = PNGEncoder.encode(img); // 见参考中的 as3corelib
- uper.load(ur);
php 接收数据保存图片代码:
- $uuid = uniqid();
- $path = sprintf('upload/%s/%s/%s/', date('Y'), date('m'), date('d'));
- $file = sprintf('%s%s.png', $path, $uuid);
- if(!file_exists($path))
- {
- mkdir($path, 0755, true);
- }
- $img = file_get_contents('php://input');
- $fp = fopen($file, 'w');
- fwrite($fp, $img);
- fclose($fp);
- echo $file;
相关参考: