Photo capturing application using Flash and PHP

In this tutorial am going to explain how to create a webcam photo capturing application using Flash and PHP. This application works on flash platform. Flash will capture the photo and send to PHP to save it in web directory. I will also explain you the action script used to develop this application.

Online photo capturing application using flash and PHP

Photo capturing application using Flash and PHP

Basically it will create a video and get a bitmap image from it. The JPGE encoder will make it as a jpg image and send it to PHP. PHP will get image from flash using $GLOBALS[“HTTP_RAW_POST_DATA”] and save it in a directory.
You can view demo and download the example file below:

Download FileDemo

Below is the Action Script of flash for this application.

import flash.display.Bitmap;
import flash.display.BitmapData;
import com.adobe.images.JPGEncoder;

//Sound for the "Capture" button click
var snd:Sound = new camerasound(); 

//Set the maximum amount of bandwidth that the current outgoing video feed can use
//Should be in bytes per second.
var bandwidth:int = 0; 

// This value is 0-100 with 1 being the lowest quality. 
var quality:int = 100;

//Run Camera
var cam:Camera = Camera.getCamera();
cam.setQuality(bandwidth, quality);

//Now setMode(videoWidth, videoHeight, video fps, favor area)
cam.setMode(320,240,30,false); 
var video:Video = new Video();
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);

//Image Data
var bitmapData:BitmapData = new BitmapData(video.width,video.height);

var bitmap:Bitmap = new Bitmap(bitmapData);

//Set height and width for image
bitmap.x = 360;
bitmap.y = 20;
addChild(bitmap);

capture_mc.buttonMode = true;

//OnClick image captur
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

function captureImage(e:MouseEvent):void {
	snd.play();
	bitmapData.draw(video);
	save_mc.buttonMode = true;
	save_mc.addEventListener(MouseEvent.CLICK, onSaveJPG);
	save_mc.alpha = 1;
}

save_mc.alpha = .5;


function onSaveJPG(e:Event):void{
	var myEncoder:JPGEncoder = new JPGEncoder(100);
	var byteArray:ByteArray = myEncoder.encode(bitmapData);
	
	var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
	
	//PHP fle to save the image
	var saveJPG:URLRequest = new URLRequest("save.php");
	saveJPG.requestHeaders.push(header);
	saveJPG.method = URLRequestMethod.POST;
	saveJPG.data = byteArray;
	
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.addEventListener(Event.COMPLETE, sendComplete);
	urlLoader.load(saveJPG);
	
	function sendComplete(event:Event):void{
		warn.visible = true;
		addChild(warn);
		warn.addEventListener(MouseEvent.MOUSE_DOWN, warnDown);
		warn.buttonMode = true;
	}

}

Now flash captured image will send to save.php. Below is the php script to save this image in a directory.

//Now get the image form flash through HTTP_RAW_POST_DATA
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
	$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
	$img = $_GET["img"];
	//image Directory
	$filename = "images/webinfopedia_". mktime(). ".jpg";
	file_put_contents($filename, $jpg);
} else{
	//show error if image is not recived 
	echo "Encoded JPEG information not received.";
}

Hope that this will help you..

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>