A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Sending image data from Flash to PHP. Help!

Threaded View

  1. #1
    Member
    Join Date
    Sep 2007
    Posts
    69

    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:DisplayObjectx:Numbery:Numberw:Numberh:Number) {
                
    _container container as DisplayObject;
                            
                
    bmp = new BitmapData(whfalse);
                
    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(containermatrix, new ColorTransform(), null, new Rectangle(00wh));
                
                
    timer.addEventListener(TimerEvent.TIMERcopySource);
                
    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 0bmp.widtha++) {
                    
    pixel bmp.getPixel(arecord.row);
                    
    str_pixel pixel.toString(16);
                    if (
    pixel == 0xFFFFFFstr_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_COMPLETErecord));
                    
    bmp.dispose();
                }
            }
            
        }    
        

    Code in the FLA file (test.fla):

    PHP Code:
    imageProcess = new JPGExporter(mySprite00mySprite.widthmySprite.height);
        
    imageProcess.addEventListener(EventImgProcess.IMAGE_COMPLETEonImageComplete);

    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($img000xFFFFFF);

    $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($hex02));
                
    $g hexdec(substr($hex22));
                
    $b hexdec(substr($hex42));
                
    // 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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center