태그 보관물: GIFEncoder

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を使いました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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<myGIFStream.length; k++){
		dataArray.writeByte(myGIFStream[k]);
	}	
	dataArray.writeMultiByte(formData2,"ascii");
 
	// Initializes an URLRequest object
	var ur:URLRequest = new URLRequest();
	ur.requestHeaders.push(urh);
	ur.method = URLRequestMethod.POST;
	ur.contentType = "multipart/form-data; boundary=" + BOUNDARY;
	ur.url = serverPath + "test/uploadok.php";
	ur.data = dataArray;
 
	ul.load(ur);
 
}// end uploadGIF
 
/**
 * Executes when a file upload is finished.
 */
function saveDone(evt:Event):void{
	var str:String = evt.target.data;
	trace(str);
}// end saveDone
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 코드의 기틀을 만들고, 이 소스를 공개하는데 흔쾌히 동의하신, 김응 실장님에게 진심으로 감사드립니다.