Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Send and Receive ByteArray to AMFPHP

Introduction

In a previous tutorial I explained how to send a bitmapdata content to the server getting value of each pixel in a BitmapData instance. Now, thanks to ActionScript3 new object ByteArray and the latest release of AMFPHP we can easily transfer a bitmapdata object using just a bytearray.. nothing else.

1. Requirements

What you need to complete this tutorial:

Moreover you need to read first this tutorial if you are not familiar with RemoteObject in Flex2.

2. AMFPHP project setup

First of all let's configure our server environment. Into you amfphp/services server folder create a directory: "tutorials/amfphp_bytearray". Move to into that folder and create a php file named "SaveJPEG.php" with this code:

				
<?php

class SaveJPEG
{
    var 
$output_dir "temp";
    var 
$server_url "http://www.sephiroth.it/amfphp2/services/tutorials/amfphp_bytearray/";

    
/**
     * Save image from the given bytearray
     * and return the path of the saved image
     */
    
function SaveAsJPEG($ba$compressed false)
    {
        if(!
file_exists($this->output_dir) || !is_writeable($this->output_dir))
            
trigger_error ("please create a 'temp' directory first with write access"E_USER_ERROR);

        
$data $ba->data;
        if(
$compressed)
        {
            if(
function_exists(gzuncompress))
            {
                
$data gzuncompress($data);
            } else {
                
trigger_error ("gzuncompress method does not exists, please send uncompressed data"E_USER_ERROR);
            }
        }
        
file_put_contents($this->output_dir "/rawdata.jpeg"$data);
        return 
$this->server_url $this->output_dir "/rawdata.jpeg";
    }

    
/**
     * Save file from a given bytearray
     * and return a ByteArray from the saved file
     */
    
function SaveAsByteArray($ba$compresses false)
    {
        if(!
file_exists($this->output_dir) || !is_writeable($this->output_dir))
            
trigger_error ("please create a 'temp' directory first with write access"E_USER_ERROR);

        
$data $ba->data;
        if(
$compressed)
        {
            if(
function_exists(gzuncompress))
            {
                
$data gzuncompress($data);
            } else {
                
trigger_error ("gzuncompress method does not exists, please send uncompressed data"E_USER_ERROR);
            }
        }
        
file_put_contents($this->output_dir "/rawdata.rgb"$data);
        return new 
ByteArray(file_get_contents($this->output_dir "/rawdata.rgb"));
    }
}

?>
In the same "amfphp_bytearray" folder create an empty folder "temp" with access mask 755. We will use that folder to store the temporary files which come from flash.

This class has just 2 methods: SaveAsJPEG and SaveAsByteArray. Both accept 2 parameters:

  • A bytearray instance
  • a parameter which tells to us if the bytearray is compressed
Because the ByteArray compress method uses zlib algorithm, you need to have a PHP installation with gzuncompress enabled.

2.1 SaveAsJPEG

This method will receive from flash a bytearray object containing an encoded jpeg.
For this reason, after we save the file as 'rawdata.jpeg', the image will be a valid jpeg file and we will just tell to flash to load this as a regular image

2.2 SaveAsByteArray

In this reason we will save the bytearray as it is, for this reason the image could not be loaded as regular image file (not you will be able to see it with a browser).
So, in this second example we will return to flash a php ByteArray instance containing the data of the saved file (using file_get_content)
Flash will receive this as a builtin bytearray instance and so we can easily use it to recreate the internal original BitmapData.