package {
// import com.adobe.p_w_picpaths.JPGEncoder;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.Sprite;
import fl.controls.Button;
import flash.events.Event;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.events.DataEvent;
import flash.events.MouseEvent;
// import com.adobe.p_w_picpaths.PNGEncoder;
import flash.geom.Point;
import flash.geom.Rectangle;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.codec.PNGEncoder;
import flash.net.*;
public class tupian extends Sprite
{
private var _sebtn:Button; //选择按钮
private var _upbtn:Button; //上传按钮
private var _loader:Loader; //选择文件临时存储
private var _cutContainer:Sprite; //存放裁剪过的图片容器
private var _primitiveContainer:Sprite; //存放原始图片的容器
private var _frame:Shape; //裁剪框
private var _downPoint:Point = new Point();
private var bytearr:ByteArray = new ByteArray();
private var _fileRef:FileReference; //打开文件夹
public function tupian()
{
init();
}
//初始化
private function init():void
{
//选择按钮
_sebtn = new Button();
_sebtn.label = "选择图片";
addChild(_sebtn);
_sebtn.addEventListener(MouseEvent.CLICK, clickHandler);
//确认按钮
_upbtn = new Button();
_upbtn.label = "确认上传";
_upbtn.x = 110;
addChild(_upbtn);
_upbtn.addEventListener(MouseEvent.CLICK, resizeImage);
//加载临时图片
_loader = new Loader();
//添加各种容器
_cutContainer = new Sprite();
_primitiveContainer = new Sprite();
_frame = new Shape();
this.addChild(_primitiveContainer);
this.addChild(_cutContainer);
this.addChild(_frame);
_primitiveContainer.y = 30;
_cutContainer.x = 300;
_cutContainer.y = 30;
trace(_cutContainer.y);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_primitiveContainer.addChild(_loader);
_primitiveContainer.addEventListener(MouseEvent.MOUSE_DOWN,onStageDown);
_primitiveContainer.addEventListener(MouseEvent.MOUSE_UP,onStageUp);
}
//设置打开文件支持所有类型
private function getImageFilter():FileFilter
{
return new FileFilter("支持的图片类型(*.jpg;*.jpeg;*.gif;*.png)", "*.jpg;*.jpeg;*.gif;*.png");
}
//设置打开文件支持单一类型
private function getFilterTypes():Array {
return [
getImageFilter(),
new FileFilter("GIF 文件 (*.gif)", "*.gif"),
new FileFilter("PNG 文件 (*.png)", "*.png"),
new FileFilter("JPG 文件 (*.jpg)", "*.jpg;*.jpeg")
];
}
//
private function clickHandler(evt:MouseEvent):void {
_fileRef = null;
_fileRef = new FileReference();
_fileRef.addEventListener(Event.SELECT, selectHandler, false, 0, true);
_fileRef.addEventListener(Event.COMPLETE, false, 0, true);
_fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadImageCompleteHandler, false, 0, true);
_fileRef.browse(getFilterTypes());
}
private function selectHandler(evt:Event):void {
_fileRef.removeEventListener(Event.SELECT, selectHandler);
_fileRef.load();
}
private function {
_fileRef.removeEventListener(Event.COMPLETE,
_loader.loadBytes(_fileRef.data);
}
private function uploadImageCompleteHandler(evt:DataEvent):void {
trace(evt.data);
_fileRef.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadImageCompleteHandler);
_fileRef = null;
}
//
//选择框
private function onStageDown(e:MouseEvent):void
{
while(_cutContainer.numChildren>0){
_cutContainer.removeChildAt(0);
}
_downPoint.x = mouseX;
_downPoint.y = mouseY;
_frame.graphics.clear();
_primitiveContainer.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
}
private function onMove(e:MouseEvent):void
{
_frame.graphics.clear();
_frame.graphics.lineStyle(1,0xff00ff);
_frame.graphics.drawRect(0, 0, mouseX - _downPoint.x, mouseY - _downPoint.y);
_frame.x = _downPoint.x;
_frame.y = _downPoint.y;
}
private function onStageUp(e:MouseEvent):void
{
_primitiveContainer.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
if (_frame.width != 0)
{
var bmpd1:BitmapData = new BitmapData(_primitiveContainer.width, _primitiveContainer.height);
bmpd1.draw(_primitiveContainer);
bytearr = bmpd1.getPixels(new Rectangle(_frame.x, _frame.y, _frame.width, _frame.height));
bytearr.position = 0;
var bmpd2:BitmapData = new BitmapData(_frame.width, _frame.height);
try
{
bmpd2.setPixels(new Rectangle(0,0,_frame.width,_frame.height),bytearr);
}
catch (err:Error)
{
trace(err.message);//若裁剪框拉的范围超出原始图像范围会报此错,若解决,需深究.
_frame.graphics.clear();
return;
}
var _bmp:Bitmap = new Bitmap(bmpd2);
_cutContainer.addChild(_bmp);
}
}
private function onComplete(e:Event):void
{
_loader.scaleX = _loader.scaleY = 0.6;
}
//选择框结束
//获取图片压缩JPG并以二进制POST给PHP
private function resizeImage(e:MouseEvent):void
{
//获取裁剪窗器并转换图片
var bitmapDate:BitmapData = new BitmapData(_cutContainer.width, _cutContainer.height, true, 0);
bitmapDate.draw(_cutContainer);
//设置图片为jpg格式,压缩率为80;
var encoder:JPEGEncoder = new JPEGEncoder(86);
var bytes:ByteArray = encoder.encode(bitmapDate);
//设置PHP接收数据
var req:URLRequest = new URLRequest("http://127.0.0.1/asjpg/upload2.php");
req.data = bytes;
req.method = URLRequestMethod.POST;
req.contentType = "application/octet-stream";
//返回接收数据
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(req);
loader.addEventListener(Event.COMPLETE, completeHandler);
}
//获取PHP返回数据
private function completeHandler(evt:Event):void
{
trace(evt.target.data);
}
}
}
<!--?php
$filename="teststream.jpg";//要生成的图片名字
$xmlstr = $GLOBALS[HTTP_RAW_POST_DATA];
if(emptyempty($xmlstr)) {
$xmlstr = file_get_contents('php://input');
}
$jpg = $xmlstr;//得到post过来的二进制原始数据
$file = fopen("p_w_picpath/".$filename,"w");//打开文件准备写入
fwrite($file,$jpg);//写入
fclose($file);//关闭
echo"ok~~~";
?-->
转载于:https://blog.51cto.com/vini123/927188