|
-
Sending image data from Flash to PHP. Help!
I want to allow the user of a flash movie to display on the browser (or to force download) an image within a Sprite, as a JPG image/file.
I started from a pre-made class located on sephiroth.it:
http://www.sephiroth.it/tutorials/fl.../print_screen/
but as this code was made in AS2, I decided to create my own AS3 class based on this one, and the result I get on the browser is a one pixel column of 1x300 pixels (the sprite size is 300x300). For some reason, the data passed from Flash to PHP is not processed right.
This is the code I made:
JPGExporter class (JPGExporter.as):
PHP Code:
package { import flash.display.BitmapData; import flash.geom.Rectangle; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.utils.Timer; import flash.events.TimerEvent; import EventImgProcess; // custom event class for events of image processing import flash.events.EventDispatcher; import flash.display.DisplayObject; import flash.net.URLVariables; public class JPGExporter extends EventDispatcher { private var timer:Timer = new Timer(5); public var record:URLVariables; // used to stored all the data to send private var _container:DisplayObject; private var bmp:BitmapData; public function JPGExporter(container:DisplayObject, x:Number, y:Number, w:Number, h:Number) { _container = container as DisplayObject; bmp = new BitmapData(w, h, false); record = new URLVariables(); record.width = w; record.height = h; record.col = 0; record.row = 0; var matrix:Matrix = new Matrix(); matrix.translate(-x, -y); bmp.draw(container, matrix, new ColorTransform(), null, new Rectangle(0, 0, w, h)); timer.addEventListener(TimerEvent.TIMER, copySource); timer.start(); } private function copySource(e:TimerEvent):void { var pixel:Number; var str_pixel:String; record["px" + record.row] = new Array(); // Get row specified by row property of record (URLVariables) for (var a:uint = 0; a < bmp.width; a++) { pixel = bmp.getPixel(a, record.row); str_pixel = pixel.toString(16); if (pixel == 0xFFFFFF) str_pixel = ""; // don't send blank pixels record["px" + record.row].push(str_pixel); } record.row += 1; if (record.row >= bmp.height) { timer.stop(); // trigger the complete event and pass through it the processed data in order to send it dispatchEvent(new EventImgProcess(EventImgProcess.IMAGE_COMPLETE, record)); bmp.dispose(); } } } }
Code in the FLA file (test.fla):
PHP Code:
imageProcess = new JPGExporter(mySprite, 0, 0, mySprite.width, mySprite.height); imageProcess.addEventListener(EventImgProcess.IMAGE_COMPLETE, onImageComplete);
function onImageComplete(e:EventImgProcess):void { var variables:URLVariables = e.variables as URLVariables; // I get the URLVariables instance containing all the data through my custom event var request:URLRequest = new URLRequest("http://localhost/.../pixels2JPG.php"); request.method = URLRequestMethod.POST; request.contentType = "application/x-www-form-urlencoded"; request.data = variables; trace("Image processing complete"); navigateToURL(request, "_blank"); }
And the PHP (pixels2JPG.php):
PHP Code:
<?php
error_reporting(0); /** * Get the width and height of the destination image * from the POST variables and convert them into * integer values */ $w = (int)$_POST['width']; $h = (int)$_POST['height'];
// create the image with desired width and height
$img = imagecreatetruecolor($w, $h);
// now fill the image with blank color // do you remember i wont pass the 0xFFFFFF pixels // from flash? imagefill($img, 0, 0, 0xFFFFFF);
$rows = 0; $cols = 0;
// now process every POST variable which // contains a pixel color for($rows = 0; $rows < $h; $rows++){ // convert the string into an array of n elements $c_row = explode(",", $_POST['px' . $rows]); for($cols = 0; $cols < $w; $cols++){ // get the single pixel color value $value = $c_row[$cols]; // if value is not empty (empty values are the blank pixels) if($value != ""){ // get the hexadecimal string (must be 6 chars length) // so add the missing chars if needed $hex = $value; while(strlen($hex) < 6){ $hex = "0" . $hex; } // convert value from HEX to RGB $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); // allocate the new color // N.B. teorically if a color was already allocated // we dont need to allocate another time // but this is only an example $test = imagecolorallocate($img, $r, $g, $b); // and paste that color into the image // at the correct position imagesetpixel($img, $cols, $rows, $test); } } }
// print out the correct header to the browser header("Content-type:image/jpeg"); // display the image imagejpeg($img, "", 90); ?>
What do you think is going wrong?
Last edited by Jazztronik; 07-17-2009 at 11:17 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|