Saving an image with AS3 and PHP: 3. ByteArray

작성자

카테고리:

The following ActionScript 3 code is used to save GIF image that is encoded using GIFEncoder. I use GIFEncoder, distributed via ByteArray.org, to encode GIF files.
다음은 GIFEncoder를 이용하여 인코딩한 GIF 파일을 저장하는 액션스크립트3 코드입니다. GIF 파일을 인코딩하기 위해서, ByteArray.org에서 배포하는 GIFEncoder를 사용하였습니다.
これはGIFEncoderを使用してGIFファイルを保存するActionScript3のコードです。GIFファイルをエンコードするために、ByteArray.comで配布されているGIFEncoderを使いました。
// Imports the GIFEndocer class by Thibault Imbert.
// You can download it from ByteArray.org.
import org.gif.encoder.GIFEncoder;

var serverPath:String = "/";

// Initializes GIFEncoder
var myGIFEncoder:GIFEncoder = new GIFEncoder();
myGIFEncoder.setRepeat(0);
myGIFEncoder.setDelay (300);

// Initializes an URLLoader object to upload a file stream.
var ul:URLLoader = new URLLoader();
ul.addEventListener(Event.COMPLETE, saveDone);

var urh:URLRequestHeader = new URLRequestHeader("Accept","image/gif");
var BOUNDARY:String = "---------------------------7d76d1b56035e";

/**
 * Starts the gif encoding when clicks on the stage.
 */
stage.addEventListener(MouseEvent.CLICK, generateGIF);
function generateGIF(evt:MouseEvent):void{
	
	// Starts the gif encoder.
	myGIFEncoder.start();
	
	// Add frame.
	var bd:BitmapData = new BitmapData(100, 100, false, Math.round(0xFFFFFF*Math.random()));
	myGIFEncoder.addFrame(bd);
	
	// Finishes the gif encoder.
	myGIFEncoder.finish();
	
	// Starts the upload process.
	uploadGIF();
	
}// end generateGIF

/**
 * Generates a file stream and upload it.
 */
function uploadGIF():void{
	
	// Retrieves the file stream from the GIF Encoder.
	var myGIFStream:ByteArray = myGIFEncoder.stream;
	var filename:String = "test.gif";
	
	// Creates the file header.
	var formData:String  =  "--" + BOUNDARY + '\r\nContent-Disposition: form-data; name="Filedata"; filename="' + filename + '"\r\nContent-Type: application/octet-stream\r\n\r\n';
	var formData2:String = "\r\n";
	formData2 += "--" + BOUNDARY + '\r\nContent-Disposition: form-data; name="Filedata"\r\n\r\nSubmit Query\r\n';
	formData2 += "--" + BOUNDARY + '--';
	
	
	// Conbines the header and the file stream.
	var dataArray:ByteArray = new ByteArray();
	dataArray.writeMultiByte(formData,"ascii");
	for(var k:uint=0; k
Special thanks to Kim Eung. He devised the draft of the PHP and AS3 codes. Furthermore, he allowed me to open them to the public.
이 PHP와 AS3 코드의 기틀을 만들고, 이 소스를 공개하는데 흔쾌히 동의하신, 김응 실장님에게 진심으로 감사드립니다.

코멘트

5의 “Saving an image with AS3 and PHP: 3. ByteArray”에 대한 답변

  1. 윤훈남 아바타

    확장하면 꽤나 확장 가능한 부분이군요. 오래전에 C++로 비슷한 엔진을 제작한적이 있는데 갑자기 문득 그 생각이 나네요.

    역시 어떤 언어든지 깊게하다보면 어떻게든 구현이 된다는거죠 ㅎㅎ

  2. Hangun's World 아바타

    네, 특히 AS 3.0에서부터 바이트어레이를 지원하니, 플래시에서 할 수 있는 것들이 더욱 더 많아진 것 같네요.

  3. gino8080 아바타
    gino8080

    how to save without PHP only with thew new flash player 10 FileReference?

  4. Hangun's World 아바타

    You can use FileReference.save() method to save a file to the local system without PHP.

    var ba:ByteArray = [Encoded ByteArray data];
    FileReference.save(ba, "image.jpg");