2

I use this code to convert an image to a BitmapData and store a JPG in a ByteArray.

import com.adobe.images.JPGEncoder;

var jpgSource:BitmapData = new BitmapData (img_mc.width, img_mc.height);
jpgSource.draw(img_mc);

var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

// here we need some code to send the bytearray but I lack enough knowledge to do it by myself

Now, I want to do the following: 1. send the ByteArray to PHP; 2. PHP must store a physical image_id.jpg on server; 3. then PHP must return the URL of the image to Flash;

Is this possible? How?

The first lines of PHP could be:

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
    // get bytearray
    $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];

    // but I don't know how to save the image on disk and how to return the URL of the //image
}

Thanks!

Lotus
  • 21
  • 1
  • 1
  • 2

1 Answers1

3

the as3 part:


import com.adobe.images.JPGEncoder;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequest;

var jpgSource:BitmapData = new BitmapData(img_mc.width,img_mc.height); jpgSource.draw(img_mc); var jpgEncoder:JPGEncoder = new JPGEncoder(85); var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

//set the request's header,method and data var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream"); var loader:URLLoader = new URLLoader(); //sends jpg bytes to saveJPG.php script var myRequest:URLRequest = new URLRequest("saveJPG.php"); myRequest.requestHeaders.push(header); myRequest.method = URLRequestMethod.POST; myRequest.data = jpgStream; loader.load(myRequest); //fire complete event; loader.addEventListener(Event.COMPLETE,saved); function saved(e:Event) { //trace the image file name trace(loader.data); }

the php (saveJPG.php) part:


if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

//the image file name   
$fileName = 'img.jpg';

// get the binary stream
$im = $GLOBALS["HTTP_RAW_POST_DATA"];

//write it
$fp = fopen($fileName, 'wb');
fwrite($fp, $im);
fclose($fp);

//echo the fileName;
echo $fileName;

} else echo 'result=An error occured.';

isa
  • 1,055
  • 16
  • 26
  • See http://stackoverflow.com/questions/2731297/file-get-contentsphp-input-or-http-raw-post-data-which-one-is-better-to for the preferred way (using the php://input wrapper) instead of HTTP_RAW_POST_DATA. – Jimmy Shelter Oct 15 '12 at 10:19
  • Wouldn´t it be better to send the request in a binary format like this? `loader.dataFormat = URLLoaderDataFormat.BINARY;` – Pablo S G Pacheco Mar 21 '13 at 11:37