I've to send 300 bitmap datas to a PHP file which saves them as PNGs. I can't just add an addEventListener
and Event.COMPLETE
. I'm parsing and creating those bitmap datas in some loops and when a bitmap got created, it sends it to the PHP file. The problem is that it works fine for the first 130+/- images but then I keep getting this error:
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error
Is there a way to send the data asynchronously? Cause I think the PHP file can't handle so many requests all at once.
Here's the code:
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import com.*;
var loadFurni = new Object();
var loaderID = 0;
var furniName = "tv_luxus";
var furniSwf = "swf/"+furniName+".swf";
loadFurni[loaderID] = new Loader();
var swfRequest = new URLRequest(furniSwf);
loadFurni[loaderID].load(swfRequest);
loadFurni[loaderID].contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(event:Event){
var getAssetXML = event.target.applicationDomain.getDefinition(furniName+"_"+furniName+"_assets");
getAssetXML = new getAssetXML;
var assets = new XML(getAssetXML);
var getVisualXML = event.target.applicationDomain.getDefinition(furniName+"_"+furniName+"_visualization");
getVisualXML = new getVisualXML;
var visualization = new XML(getVisualXML);
var colorsStr = "";
var colors = colorsStr.split(",");
//possible sizes
var furniSizes = new Array(32, 64);
for each(var furniSize in furniSizes){
var furniBgWidth;
var furniBgHeight;
var furniBg:Bitmap;
var furniBgX = 0;
var furniBgY = 0;
var visualizationID;
var layerCount;
var dirs = new Array();
var blendModes = new Array();
var alphaModes = new Array();
var zModes = new Array();
var paddingBottom = 30;
//getting the layercount + visualizationID
for(var l=0; l<visualization.visualization.length(); l++){
if(visualization.visualization[l].@size == furniSize){
layerCount = visualization.visualization[l].@layerCount;
visualizationID = l;
}
}
//now searching the possible directions
for(var d=0; d<visualization.visualization[visualizationID].directions.direction.length(); d++){
dirs.push(visualization.visualization[visualizationID].directions.direction[d].@id);
}
for each(var dir in dirs){
var animations = new Array();
//searching the possible animations
for(var ta=0; ta<visualization.visualization[visualizationID].animations.animation.length(); ta++){
animations.push(visualization.visualization[visualizationID].animations.animation[ta].@id);
}
//if there's no animation
if(animations[0] == undefined){ animations = new Array("0"); }
for each(var animation in animations){
//getting layersinformations such as blendmodes, zindex and alpha
var layers = visualization.visualization[visualizationID].layers[0];
if(layers != undefined && layers.layer[0] != undefined){
for each(var layer in layers.layer){
blendModes[layer.@id] = (layer.@ink!= undefined) ? layer.@ink : "NORMAL";
alphaModes[layer.@id] = (layer.@alpha!= undefined) ? int(layer.@alpha): 255;
zModes[layer.@id] = (layer.@z!= undefined) ? int(layer.@z): 0;
}
}
//zIndex fix. sometimes the zindex is saved in the <direction/>
for(var zfix=0; zfix<visualization.visualization[visualizationID].directions.direction.length(); zfix++){
if(visualization.visualization[visualizationID].directions.direction[zfix].@id == dir){
for(var zfixl=0; zfixl<visualization.visualization[visualizationID].directions.direction[zfix].layer.length(); zfixl++){
if(visualization.visualization[visualizationID].directions.direction[zfix].layer[zfixl].@z != undefined){
zModes[visualization.visualization[visualizationID].directions.direction[zfix].layer[zfixl].@id] = visualization.visualization[visualizationID].directions.direction[zfix].layer[zfixl].@z;
}
}
}
}
var frames = new Array();
//settings the default frames
if(visualization.visualization[visualizationID].animations != undefined && visualization.visualization[visualizationID].animations.animation != undefined){
for each(var animationLayer in visualization.visualization[visualizationID].animations.animation[0].animationLayer){
frames[animationLayer.@id] = new Array( );
//the problem is, that sometimes there aint no framesequences. so we just add 0 as default.
if(animationLayer.frameSequence !=undefined){
for each(var sframe in animationLayer.frameSequence.frame){
frames[animationLayer.@id].push(sframe.@id);
}
}
else {
frames[animationLayer.@id].push(0);
}
}
}
//overwriting the stuff with the animation layers
if(animation>0){
for each(animationLayer in visualization.visualization[visualizationID].animations.animation[animation].animationLayer){
if(animationLayer.frameSequence !=undefined){
if(frames[animationLayer.@id] !=undefined){
//clearing the array, so we can overwrite it.
frames[animationLayer.@id].splice(0);
for each(sframe in animationLayer.frameSequence.frame){
frames[animationLayer.@id].push(sframe.@id);
}
}
else {
//creating a new array
frames[animationLayer.@id].push(0);
}
}
}
}
//count lowest framerate
var lowestFrameRate = 9999;
for(var low in frames){
if(lowestFrameRate > frames[low].length){
lowestFrameRate = frames[low].length;
}
}
if(lowestFrameRate == 9999){
lowestFrameRate=0;
}
//count highest framerate
var highestFrameRate = 0;
if(frames[lowestFrameRate] != undefined){
for(var high in frames){
if(highestFrameRate < frames[high].length){
highestFrameRate = frames[high].length;
}
}
}
if(!highestFrameRate){
highestFrameRate=1;
}
for(var frame=0; frame<(highestFrameRate); frame++){
//trace("[size: "+furniSize+"; dir: "+dir+"; animation: "+animation+"; frame: "+frame+"] {");
var furniParts = new Array();
var cordHX = 0;
var cordLX = 10000;
var cordHY = 0;
var cordLY = 10000;
var cordHYC =0;
var actualFrame;
for(var i = 0; i<layerCount; i++){
if(frames[i] == undefined) actualFrame = 0;
else actualFrame = (!frames[i][frame]) ? frames[i][0] : frames[i][frame];
for each(var asset in assets.asset){
var furniClass = furniName+"_"+furniSize+"_"+String.fromCharCode(97+i)+"_"+dir+"_"+actualFrame;
if(asset.@name == furniClass){
var flip = false;
if(asset.@flipH == "1") {
furniClass = asset.@source;
flip = true;
}
else if(asset.@source != undefined){
furniClass = asset.@source;
}
//trace(furniClass);
var bmp;
if(event.target.applicationDomain.hasDefinition(furniName+"_"+furniClass)){
bmp = event.target.applicationDomain.getDefinition(furniName+"_"+furniClass);
bmp = new bmp;
}
else { trace("CONTINUED"); continue; };
if(((asset.@x*-1) + (bmp["width"]*1)) > cordHX){
cordHX = (asset.@x*-1) + (bmp["width"]*1);
}
if(((asset.@x*-1) < cordLX)){
cordLX = (asset.@x*-1);
}
if(((asset.@y*-1) + (bmp["height"]*1)) > cordHY){
cordHY = (asset.@y*-1) + (bmp["height"]*1);
}
if(((asset.@y*-1) < cordLY)){
cordLY = (asset.@y*-1);
}
if(((asset.@y*-1) < cordHYC)){
cordHYC = (asset.@y*-1);
}
//might not always exist, so we check and set 255 for sure.
if(!alphaModes[i]) alphaModes[i] = 255;
var canvas = new Bitmap();
canvas.bitmapData = new BitmapData(bmp["width"], bmp["height"], true, 0);
//draws the image on the canvas + alpha
var matrix = new Matrix();
var transforming = new ColorTransform();
transforming.alphaMultiplier = (alphaModes[i]/255);
canvas.bitmapData.draw(bmp, matrix, transforming);
//redraws the image on the canvas + alpha + color
if(colors[i]){
var coloring = new ColorTransform();
coloring.color = ("0x"+colors[i]); //defines colors token by the colors array
coloring.alphaMultiplier = (alphaModes[i]/255);
canvas.bitmapData.draw(bmp, matrix, coloring, BlendMode.MULTIPLY);
}
if(!zModes[i]){ //fixing zModes
zModes[i] = 0;
}
//now we'll add all the data into an array(object()). later we'll build it together out of that array
furniParts.push({ "daI" : i,"daZ": int(zModes[i]), "daB": blendModes[i], "x" : int(asset.@x), "y" : int(asset.@y), "daC" : canvas });
}
}
}
var furniWidth = ((cordHX * 1 - cordLX*1));
var furniCenter = Math.abs(int(cordHX)) - Math.abs(int(cordLX));
var furniHeight = ((cordLY*1 - cordHY * 1)*-1);
var furniYFix = (cordHYC*-1);
//medium 400x400
if(furniHeight > (200-paddingBottom) || furniWidth > (200-paddingBottom)){
furniBgWidth = 400;
furniBgHeight = 400;
furniBg = new Bitmap();
var bgDataM:bgMedium = new bgMedium(furniBgWidth, furniBgHeight);
furniBg.bitmapData = bgDataM;
}
//large 600x600
else if(furniHeight > (400-paddingBottom) || furniWidth > (400-paddingBottom)){
furniBgWidth = 600;
furniBgHeight = 600;
furniBg = new Bitmap();
var bgDataL:bgLarge = new bgLarge(furniBgWidth, furniBgHeight);
furniBg.bitmapData = bgDataL;
}
//default, small
else {
furniBgWidth = 200;
furniBgHeight = 200;
furniBg = new Bitmap();
var bgDataS:bgSmall = new bgSmall(furniBgWidth, furniBgHeight);
furniBg.bitmapData = bgDataS;
}
furniParts.sortOn(["daZ","daI"], [Array.NUMERIC, Array.NUMERIC]);
//now let's build it
for each(var furniPart in furniParts){
//draws canvas on furniBg (?+ blendmode)
var finalBlendMode = furniPart.daB;
var finalCanvas = furniPart.daC;
var posx = (furniBgWidth/2) - (furniCenter/2) - furniPart.x;
var posy = furniYFix + furniBgHeight - furniHeight - paddingBottom - furniPart.y;
var daMatrix = new Matrix();
daMatrix.translate(posx,posy);
if(finalBlendMode){
if(finalBlendMode == "ADD") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.ADD);
else if(finalBlendMode == "NORMAL") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.NORMAL);
else if(finalBlendMode == "OVERLAY") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.OVERLAY);
else if(finalBlendMode == "ERASE") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.ERASE);
else if(finalBlendMode == "ALPHA") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.ALPHA);
else if(finalBlendMode == "INVERT") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.INVERT);
else if(finalBlendMode == "SUBTRACT") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.SUBTRACT);
else if(finalBlendMode == "DIFFERENCE") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.DIFFERENCE);
else if(finalBlendMode == "LIGHTEN") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.LIGHTEN);
else if(finalBlendMode == "DARKEN") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.DARKEN);
else if(finalBlendMode == "SCREEN") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.SCREEN);
else if(finalBlendMode == "MULTIPLY") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.MULTIPLY);
else if(finalBlendMode == "LAYER") furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.LAYER);
else furniBg.bitmapData.draw(finalCanvas, daMatrix, null, BlendMode.NORMAL);
}
else {
furniBg.bitmapData.draw(finalCanvas, daMatrix);
}
}
if(flip){
var flipMatrix = new Matrix();
flipMatrix.scale(-1,1);
flipMatrix.translate(furniBg.width,0);
var flipped = new Bitmap();
flipped.bitmapData = new BitmapData(furniBg.width, furniBg.height, true, 0);
flipped.bitmapData.draw(furniBg, flipMatrix);
furniBg = flipped;
//trace("dir: "+dir+" - flipped ["+furniSize+"]");
}
furniBg.x = furniBgX;
addChild(furniBg);
furniBgX = furniBgX + 200;
exportFurni(furniBg, dir, frame, animation, furniSize, furniName);
furniBg = null;
//trace("}\n\n");
}
}
}
}
}
function exportFurni(furni, var1, var2, var3, var4,var5){
var finalFurni = PNGEncoder.encode(furni.bitmapData);
var reqHeader:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var reqURL:URLRequest = new URLRequest("http://localhost/furniviewer_flash/export/exporter.php?name="+var5+"&v1="+var1+"&v2="+var2+"&v3="+var3+"&v4="+var4);
reqURL.method = URLRequestMethod.POST;
reqURL.data = finalFurni;
reqURL.requestHeaders.push(reqHeader);
var reqLoader = new URLLoader();
/* reqLoader.addEventListener(Event.COMPLETE, exported);
reqLoader.addEventListener(IOErrorEvent.IO_ERROR, notexported);
*/
reqLoader.load(reqURL);
}