SaveByteArray

Package:
Class:
Inheritance:
biz.flashscript.utils
public class SaveByteArray
Sprite
The SaveByteArray class allows to save high quality images with unique names in a folder on the server. This class requires a server side script and the Adobe image encoder classes. Use the constructor new SaveByteArray() to create an instance of the class and add parameters as desired.
Public Methods
Method Defined By
SaveByteArray()
Creates a new instance of the SaveByteArray class.
SaveByteArray
initLoader(loadFinished:Function, myURL:String, mClip:DisplayObjectContainer,qual:int=80._image:String="jpg"._width:int=1000._height:int=1000._time:int=1000.myTarget:Object=null.aNum:Object=null)
This function will trigger to create a new bitmap and save it on the server in jpg or png format.
SaveByteArray
stopTimer()
This function will stop the counting to wait for saving the image.
SaveByteArray
Events
Event Defined By
All inherited events of the Sprite class.
Constructor Detail
SaveByteArray ()  Constructor
public function SaveByteArray()
Intializes a new SaveByteArray instance.
Method Detail

initLoader()  method
public function initLoader(loadFinished:Function, myURL:String, mClip:DisplayObjectContainer, qual:int=80, _image:String="jpg", _width:int=1000, _height:int=1000, _time:int=1000, myTarget:Object=null, aNum:Object=null):void

This function will trigger to create a new bitmap and save it on the server in jpg or png format.

Parameters

  • loadFinished:Function ____ Specifies the listener function when the image is saved.
  • myURL:String ____ Specifies the url for the php file. Add a name at the end of the url and if required a folder name. A typical url looks like this: "http://yourdomain.com/imageSaver.php?name=test.jpg&folder=images".
  • mClip:DisplayObjectContainer ____ Specifies the holder for the image to be used as a template to save.
  • qual:int=80 ____ Specifies the quality of the image to be saved. Default is 80 and maximum value is 100.
  • _image:String="jpg" ____ Specifies the type of image to be created. Default is jpg and the other possibility is png.
  • _width:int=1000 ____ Specifies the width of the new image. Default is 1000 pixel.
  • _height:int=1000 ____ Specifies the height of the new image. Default is 1000 pixel.
  • _time:int=1000 ____ Specifies the time interval of the new image. Default is 1000 msec.
  • myTarget:Object=null ____ Specifies a textfield to count the waiting time. Default is null.
  • aNum:Object=null ____ Specifies any kind of parameter to transfer to the listener function. Default is null.

stopTimer()  method
public function stopTimer():void

This function will stop the counting to wait for saving the image.

Examples
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. Place 4 button components and name them A0, A1, A2, A3. 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 var A0:Button;
		public var A1:Button;
		public var A2:Button;
		public var A3:Button;

		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 ();
			//
			A0.label = "image 1";
			A0.addEventListener (MouseEvent.CLICK, selectImage);
			A1.label = "image 2";
			A1.addEventListener (MouseEvent.CLICK, selectImage);
			A2.label = "image 3";
			A2.addEventListener (MouseEvent.CLICK, selectImage);
			A3.label = "image 4";
			A3.addEventListener (MouseEvent.CLICK, selectImage);
			submitBut.label = "Save Image";
			showImg.label = "Show image";
			showImg.visible = false;
			showImg.addEventListener (MouseEvent.CLICK, showImage);
			submitBut.addEventListener (MouseEvent.CLICK, clickHandler);
		}
		private function selectImage (ev:Event):void
		{
			imgHolder.source = "http://flashscript.biz/flashas3/biz/images/"+ev.currentTarget.name+".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.htmlText = _result;
			if (_result == "File is not correct!")
			{
				navigateToURL (new URLRequest("http://flashscript.biz/flashas3/savebytearray/images/failure.jpg"));
			}
			else
			{
				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;
?>