Hi!
I have a problem when creating a image with GD from a HEX value (bitmap) returned from a flash aplication (using flash.display.BitmapData Class to return a image captured from the visitors webcam). The "Live" picture in the flash aplication is looking good, and the picture displayed right after the picture is taken (still in the flash program) is looking good, but after I have passed the image to the PHP script, and then returns the image to the browser, it almost looks like an old fashion negative photo, where dark areas has become violet.
Does anyone see something to improve in the following code, or coud someone instruct me how to re-write the code to use ImageMagick?
/*
///////////////////////////////////////////////
EXAMPLE: Webcam Snapshot
AUTHOR: Robert M. Hall
CREATED: September 1st, 2005
MODIFIED: September 6th, 2005
ASBE == Great Software Examples
http://www.ifbin.com/asbe
///////////////////////////////////////////////
*/
/*
FUNCTIONALITY:
Utilize the new bitmap features, draw method of movieclip and camera API and a
server side PHP script to take individual snapshots from the locally attached webcam
and generate standalone JPEGs
USAGE:
Great for kiosk style applications to allow sending of email postcards with image snapshots
without requiring Flash Communications server or other expensive software
*/
// convert the six digit hex values for our pixel color data into the individual
// Red,Green,Blue values to allow us to re-create our image data
function hex2int( $hex ) {
return array( 'r' => hexdec( substr( $hex , 0 , 2 ) ), // 1st pair of digits
'g' => hexdec( substr( $hex , 2 , 2 ) ), // 2nd pair
'b' => hexdec( substr( $hex , 4 , 2 ) ) // 3rd pair
);
}
// Split apart the POST data on commas and put it into an array
$img_arr=explode( "," , $_POST["pixels_arr"] );
// Check to make sure we received the image width from the camera
// if not set it to default value
if( !isset( $_POST["pixels_width"] ) ) {
$img_width=319;
} else {
$img_width=$_POST["pixels_width"];
}
// Check to make sure we received the image height from the camera
// if not set it to default value
if( !isset( $_POST["pixels_height"] ) ) {
$img_height=239;
} else {
$img_height=$_POST["pixels_height"];
}
// Output our header data to indicate to the browser we are returning a JPEG image
header( "Content-type: image/jpeg" );
// Create our JPEG object (requires GD library 2.0 in your PHP installation)
$image=imagecreatetruecolor( $img_width ,$img_height );
// Set our default background color - it will be overwritten but needs to be set first
$background = imagecolorallocate( $image , 0 , 0 , 0 );
// Temporary counter value for walking through our pixel array
$cnt=0;
// Set up our for loop to parse through the array we received in POST data from Flash with
// all our pixel color information
for ( $xx=0 ; $xx < $img_width ; $xx++ ) {
for ( $yy=0 ; $yy < $img_height ; $yy++ ) {
// Get the RGB color values from hex data
$color_arr = hex2int( $img_arr[$cnt] );
// assign the new color temporarily to allow us to color our next pixel
$newcolor = imageColorAllocate( $image , $color_arr['r'] , $color_arr['g'] , $color_arr['b'] );
// set our pixel at xx,yy ith newcolor
imagesetpixel ( $image , $xx , $yy , $newcolor );
// increase our temp counter for walking through the pixel array
$cnt++;
}
}
// create our JPEG image from the image data
// (This could also be used to output to a local file for later usage or storage
ImageJPEG( $image );
// Destroy/release memory used to create our JPEG
imagedestroy( $image );
// Thats all folks!
?>
Regards Vidar