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); // 見參考中的 as3corelibuper.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;
相關參考:
- as3corelib
- file_get_contents()
- Loader.loadBytes()
- FileReference.load()
- ByteArray