最近在做一个项目,需要用到AS3把图片上传到服务器上面。服务器语言选择PHP。于是,在网上搜索了一下,发现主流的操作是利用AS3+AMFPHP进行结合。
纠结了半天,写了一个客户端DEMO和服务器端脚本DEMO。
客户端三个辅助类:
package
{
import com.adobe.images.JPGEncoder;
import flash.display.Sprite;
import flash.net.FileFilter;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.net.FileReference;
import flash.events.Event;
import flash.display.Loader;
import flash.utils.ByteArray;
public class LoadBmp extends Sprite
{
public var smallW:Number;//
public var smallH:Number;
public var bitmap:Bitmap;
private var file:FileReference;
private var loaders:Loader;
private var com:Function;
public function LoadBmp(smallW:uint = 120, smallH:uint = 120)
{
this.bitmap = new Bitmap();
this.file = new FileReference();
this.loaders = new Loader();
this.smallW = smallW;
this.smallH = smallH;
}
private function small(smallW:Number, smallH:Number, bitmapdata:BitmapData):BitmapData
{
var xx:Number = smallW / bitmapdata.width;
var yy:Number = smallH / bitmapdata.height;
var ww:Number = (smallW - bitmapdata.width * xx) / 2;
var hh:Number = (smallH - bitmapdata.height * yy) / 2;
var matrix:Matrix = new Matrix();
matrix.scale(xx, yy);
matrix.translate(ww, hh);
var bmpdata:BitmapData = new BitmapData(smallW, smallH, false, 268435455);
bmpdata.draw(bitmapdata, matrix);
return bmpdata;
}
public function loads(callback:Function):void
{
this.file.browse([new FileFilter("只能图片啊", "*.jpg;*.png;*.gif")]);
this.file.addEventListener(Event.SELECT, this.onSelect);
com = callback;
return;
}
private function onSelect(e:Event):void
{
this.file.load();
this.file.addEventListener(Event.COMPLETE, this.onComplete);
}
private function onComplete(e:Event):void {
this.loaders.loadBytes(this.file.data);
this.loaders.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete2);
return;
}
private function onComplete2(e:Event):void
{
this.bitmap = e.target.content as Bitmap;
this.bitmap.bitmapData = this.small(this.smallW, this.smallH, this.bitmap.bitmapData);
this.bitmap.x = 0;
addChild(this.bitmap);
com();
}
}
}
package
{
import flash.display.SimpleButton;
public class CustomSimpleButton extends SimpleButton {
private var upColor:uint
= 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0x00CCFF;
private var size:uint
= 80;
public function CustomSimpleButton() {
downState
= new ButtonDisplayState(downColor, size);
overState
= new ButtonDisplayState(overColor, size);
upState
= new ButtonDisplayState(upColor, size);
hitTestState
= new ButtonDisplayState(upColor, size * 2);
hitTestState.x = -(size / 4);
hitTestState.y = hitTestState.x;
useHandCursor
= true;
}
}
}
package
{
import flash.display.Shape;
public class ButtonDisplayState extends Shape {
private var bgColor:uint;
private var size:uint;
public function ButtonDisplayState(bgColor:uint, size:uint) {
this.bgColor = bgColor;
this.size
= size;
draw();
}