2

I have the following code in my AS3 Flash code that takes a screenshot within the swf using JPGEncoder and sends it to the url where i write it to a file in PHP.

I did run into the Google Chrome Pepperflash issue recently where the function just stops and the page fails to redirect. Nothing gets sent to save.php. By changing

var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");

to

var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");

That seemed to do the trick. As of today though this works in internet Explorer but no longer in Chrome, Safari, Firefox. I saw that Adobe put out an update/patch to flash and flash player yesterday - could that have anything to do with it?

If i remove the following:

var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain"); 
jpgURLRequest.requestHeaders.push(header);   

Then the page successfully redirects but $GLOBALS['HTTP_RAW_POST_DATA'] is then empty so no image file can be created.

Is there an alternative header i can put that will solve this?

My code is:

AS3:

function createJPG(m:MovieClip, q:Number, fileName:String) {
    var jpgSource:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
    jpgSource.draw(stage);
    var jpgScreenshot: BitmapData = new BitmapData(362, 310);
    jpgScreenshot.copyPixels(jpgSource, new Rectangle(288, 89, 362, 310), new Point(0, 0));
    var jpgEncoder:JPGEncoder = new JPGEncoder(q);
    var jpgStream:ByteArray = jpgEncoder.encode(jpgScreenshot);

    var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/plain");

    var jpgURLRequest:URLRequest = new URLRequest ("http://www.url.com/save.php");      
    jpgURLRequest.requestHeaders.push(header);              
    jpgURLRequest.method = URLRequestMethod.POST;               
    jpgURLRequest.data = jpgStream;
    var jpgURLLoader:URLLoader = new URLLoader();   
    navigateToURL(jpgURLRequest, "_self");
}

save.php

$imagefile=''.$imageURL.'';
$fp = fopen($imagefile, 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);

header('Location: https://www.url.com/your-image.php');
Sam
  • 7,252
  • 16
  • 46
  • 65
odd_duck
  • 3,941
  • 7
  • 43
  • 85

3 Answers3

2

Managed to get this working now with the following code. Liam was correct on the Flash Player issue. Working now by separating saving the image and navigating to the url into 2 differents function:

function createJPG(m:MovieClip, q:Number, fileName:String) {
    var jpgSource:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
    jpgSource.draw(stage);
    var jpgScreenshot: BitmapData = new BitmapData(362, 310);
    jpgScreenshot.copyPixels(jpgSource, new Rectangle(288, 89, 362, 310), new Point(0, 0));
    var jpgEncoder:JPGEncoder = new JPGEncoder(q);
    var jpgStream:ByteArray = jpgEncoder.encode(jpgScreenshot);

    var urlLoader:URLLoader = new URLLoader();
    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    var saveJPG:URLRequest = new URLRequest('http://www.url.com/save_image.php?name=filename';
    saveJPG.requestHeaders.push(header);
    saveJPG.method = URLRequestMethod.POST;
    saveJPG.data = jpgStream;   
    urlLoader.addEventListener(Event.COMPLETE, goToCheckout);
    urlLoader.load(saveJPG);
}

function goToCheckout(e:Event):void{
    var url:String = 'http://www.url.com/show_image.php';
    var request:URLRequest = new URLRequest(url);
    try {
      navigateToURL(request, '_self'); 
    } catch (e:Error) {
      trace("Error occurred!:");
    }
}

save_image.php:

if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
    $path = "";
    $id = $_GET["name"];
    $file = $id;
    file_put_contents($path.$file, $jpg);
    echo "complete";
} else{
    // error
}
odd_duck
  • 3,941
  • 7
  • 43
  • 85
1

Flash Player version 13.0.0.214 introduces some key security fixes. Unfortunately it also breaks navigateToUrl() by forbidding the changing of any headers in the request given to navigateToUrl(). This breaks POST requests that need to pass in security/session token headers, or to even change the Content-Type, e.g. to text/xml, etc. as you have done in your example.

Right now, as much as it sucks, our best known workaround is to downgrade clients to 13.0.0.206

  • got this working now with my answer below, maybe that can help you? – odd_duck May 15 '14 at 19:03
  • A bug has been filed at Adobe because the change introduced in 13.0.0.214 may be too strict. Vote for it: https://bugbase.adobe.com/index.cfm?event=bug&id=3759971 – Chris May 19 '14 at 11:14
0

Adobe proposes using external interface call as described here: https://forums.adobe.com/message/6396080 it uses JavaScript to replace the POST method

The solution proposed by odd_duck seems simpler though - would be great to see the php code as well.

Fred
  • 49
  • 1
  • 7