Create a new fla file and name it SaveBytearrayexample.fla. Place the fla in the same folder as the biz folder. Put a UILoader component (imgHolder), two Button components (submitBut, showImg) and one TLFTextField instance (waitCounter) on the stage. Then create an Actionscript file, name it SaveBytearrayexample.as and place this script.
package
{
import flash.display.Sprite;
import flash.events.*;
import fl.containers.UILoader;
import fl.controls.Button;
import biz.flashscript.utils.SaveByteArray;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import fl.text.TLFTextField;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.TextFlow;
public class SaveBytearrayexample extends Sprite
{
public var imgHolder:UILoader;
public var submitBut:Button;
public var waitCounter:TLFTextField;
public var showImg:Button;
private var _widthSetter:TLFTextField;
private var _heightSetter:TLFTextField;
private var imgLoader:SaveByteArray;
public function SaveBytearrayexample ():void
{
_widthSetter = new TLFTextField();
_widthSetter.x = 88;
_widthSetter.y = 150;
_widthSetter.restrict = "0-9";
_widthSetter.text = "500";
_widthSetter.width = 80;
_widthSetter.height = 25;
_widthSetter.background = true;
_widthSetter.backgroundColor = 0xCCCCCC;
_widthSetter.type = "input";
addChild (_widthSetter);
_heightSetter = new TLFTextField();
_heightSetter.text = "500";
_heightSetter.x = 88;
_heightSetter.y = 180;
_heightSetter.width = 80;
_heightSetter.height = 25;
_heightSetter.background = true;
_heightSetter.backgroundColor = 0xCCCCCC;
_heightSetter.type = "input";
addChild (_heightSetter);
var myFormat:TextLayoutFormat = new TextLayoutFormat();
myFormat.color = 0x336633;
myFormat.fontFamily = "Arial, Helvetica, _sans";
myFormat.fontSize = 18;
myFormat.textAlign = "center";
var myTextFlow1:TextFlow = _widthSetter.textFlow;
myTextFlow1.hostFormat = myFormat;
myTextFlow1.flowComposer.updateAllControllers ();
var myTextFlow2:TextFlow = _heightSetter.textFlow;
myTextFlow2.hostFormat = myFormat;
myTextFlow2.flowComposer.updateAllControllers ();
submitBut.label = "Save Image";
showImg.label = "Show image";
showImg.visible = false;
showImg.addEventListener (MouseEvent.CLICK, showImage);
submitBut.addEventListener (MouseEvent.CLICK, clickHandler);
imgHolder.source = "http://flashscript.biz/flashas3/savebytearray/images/testimage.jpg";
}
private function clickHandler (ev:Event):void
{
waitCounter.text = "Saving starts in a second...";
imgLoader = new SaveByteArray();
var _url:String = "http://flashscript.biz/flashas3/savebytearray/images/imageSaver.php?name=test.jpg&folder=images";
var _width:int = int(_widthSetter.text);
var _height:int = int(_heightSetter.text);
imgLoader.initLoader (imageUrlLoaderComplete,_url,imgHolder,100,"jpg",_width,_height,1000,waitCounter);
}
private function imageUrlLoaderComplete (evt:Event=null,a:Object=null):void
{
if (evt != null)
{
var _result:String = unescape(evt.currentTarget.data.myResult);
}
if (a != null)
{
_result = String(a);
}
imgLoader.stopTimer ();
waitCounter.text = _result;
showImg.visible = true;
}
private function showImage (ev:Event):void
{
navigateToURL (new URLRequest("http://flashscript.biz/flashas3/savebytearray/images/images/test.jpg"));
}
}
}
The following php script (imageSaver.php) is located on the server as specified by the URL.
<?php
$absolutepath = dirname(__FILE__);
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] ))
{
$im = $GLOBALS["HTTP_RAW_POST_DATA"];
$filename = $_GET['name'];
$folder = $_GET['folder'];
$counter = $_GET['count'];
if(!file_exists ($folder))
{
mkdir($folder, 0777);
if (!chmod("$folder",0777))
{
fail("The permissions failed to set to 0777.");
}
}
$fullFilePath=$absolutepath."/".$folder."/".$filename;
$handle=fopen($fullFilePath,"w");
if(!$handle)
{
fail("Could not open file!.");
}
$write=fwrite($handle,$im);
if(!$write)
{
fail("Could not write to file!.");
}
fclose($handle);
success($counter);
}
else
{
fail("Could not place the file");
}
function success($counter)
{
$counter = $counter+1;
echo "myResult=$counter";
}
function fail($errmess)
{
echo "myResult=".$errmess;
}
exit;
?>
|