Hi there!
I was playing with the JPGEncoder class trying to make an AS3 image uploader. My goal was to let the user browse a file on it’s hard disk, display it in a movieclip and then use the JPGEncoder class to convert that movieclip (with the image on it) to ByteArray and send it all to a server-side script. The script (PHP) would then use the ByteArray data to create a jpg file on the server’s disk. The problem is that you can’t get the file path’s from FileReference (it’s a security thing). I would like to do this so that I could resize, let’s say, a 800x600px image to 320x240 (the size of the container movieclip) BEFORE sending it to the server…so…how could I acomplish this, if possible? I’m using AS3 with Flash.
This is the code to load and display the image in a movieclip. It works for files in the same folder as the swf:
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
var fileRef:FileReference = new FileReference();
var filetypes1:FileFilter;
var img_loader:Loader;
bt_browse.addEventListener(MouseEvent.CLICK,browseFiles,false,0,true);
function browseFiles(evt:MouseEvent){
fileRef.addEventListener(Event.SELECT,fileSelected,false,0,true);
filetypes1 = new FileFilter("Ficheiros: *.jpg,*.png", "*.jpg;*.png;*.JPG;*.PNG");
fileRef.browse(new Array(filetypes1));
}
function fileSelected(evt:Event) {
img_loader = new Loader();
trace(fileRef.name);
if(checkExtension(fileRef.name)){
var url:URLRequest = new URLRequest(fileRef.name);
img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, diskFileLoaded);
img_loader.load(url);
}
else{
trace("Invalid filetype...");
}
}
/** checks the extension **/
function checkExtension(filename):Boolean{
var res = false;
var fileext = "*" +filename.substr(filename.indexOf("."),filename.length);
if(filetypes1.extension.indexOf(fileext)!=-1){
res = true;
}
else{
res = false;
}
return res;
}
/** add the image to the movieclip, resizing it to 320x240 **/
function diskFileLoaded(e:Event):void{
img_loader.alpha = 1;
img_loader.cacheAsBitmap = true;
if(img_loader.content.width > img_loader.content.height){
img_loader.content.width = 320;
img_loader.content.scaleY = img_loader.content.scaleX;
viewer.container.x = 0;
viewer.container.y = 0;//viewer.container.height/2;// - img_loader.content.height/2;
}
else{
img_loader.content.height = 240;
img_loader.content.scaleX = img_loader.content.scaleY;
viewer.container.x = 0;//viewer.container.width/2;// - img_loader.content.width/2
viewer.container.y = 0;
}
this.alpha = 0;
viewer.container.addChild(img_loader);
this.tween = new Tween(this, "alpha", Regular.easeOut, 0, 1, 6, false);
}
Anyone?
Thanks!