Converting to JPEG

Hello everyone,

I was working with the script from:

http://www.flash-db.com/Tutorials/snapshot/

and almost got everything perfectly however; I wanted to implement a drawing board for this where user could draw something, Flash sends the information to PHP file and PHP converts it which ended up having a weird problem. It is all fine except the final image is very low-detail.

Here is the PHP code:

<?php
	//If GD library is not installed, say sorry
	if(!function_exists("imagecreate")) die("Sorry, you need GD library to run this example");
	//Capture Post data
	$data = explode(",", $_POST['img']);
	$width = $_POST['width'];
	$height = $_POST['height'];
	//Allocate image
	$image=(function_exists("imagecreatetruecolor"))?imagecreatetruecolor( $width ,$height ):imagecreate( $width ,$height );
	imagefill($image, 0, 0, 0xFFFFFF);
	//Copy pixels
	$i = 0;
	for($x=0; $x<=$width; $x++){
		for($y=0; $y<=$height; $y++){
			while(strlen($data[$i]) < 6) $data[$i] = "0" . $data[$i];
			$r = 255-hexdec("0X".substr( $data[$i] , 0 , 2 )); 
			$g = 255-hexdec("0x".substr( $data[$i] , 2 , 2 )); 
			$b = 255-hexdec("0x".substr( $data[$i++] , 4 , 2 ));
			$color =  ($r << 16) | ($g << 8) | $b; 
			$color = imagecolorallocate($image, $r, $g, $b);
			imagesetpixel ( $image , $x , $y , $color );
		}
	}
	//Output image and clean
	header( "Content-type: image/jpeg" );
	ImageJPEG( $image );
	imagedestroy( $image );	
?>

I believe there is something I can increase here or maybe there is sth that skips couple pixels but couldn’t figure it out.

And here is the AS function that gathers the pixel information:

function output(who){
//preloader visible
preloader._visible = true
//Here we will copy pixels data
var pixels:Array = new Array()
//Create a new BitmapData
var snap = new BitmapData(who.width, who.height);
myMatrix = new Matrix();
//Copy image
snap.draw(who, myMatrix);
//Uncoment this line if you want to apply filters instead of just capturing source
// results should be adjusted based on your library version, not recomended
//snap.applyFilter(snap, snap.rectangle, snap.rectangle.topLeft, myFilters[nr])
var w:Number = snap.width, tmp
var h:Number = snap.height
//Build pixels array using an onEnterframe to avoid timeouts, capture a row per iteration, show a progressbar
var a:Number = 0
this.onEnterFrame = function(){
for(var b=0; b<=h; b++){
tmp = snap.getPixel32(a, b).toString(32)
pixels.push(tmp.substr(1))
}
perc = int((a*100)/w)
preloader.perc.text = perc+" %"
preloader.barra._xscale = perc
a++
if(a>w){ //Finish capturing
preloader._visible = false
sendData(pixels, h, w)
//free memory
snap.dispose()
delete this.onEnterFrame
}
}
}

http://www.ozelegemezun.com/snapshot/snapshot.html

Try it!

I’d be glad if you can help

Thanks :nerd: