AS3: Save JPEG's from Flash


 / Published in: ActionScript 3
 

URL: http://designreviver.com/tutorials/actionscript-3-jpeg-encoder-revealed-saving-images-from-flash

I found this excellent tutorial on how to save JPEG images from Flash. You will need the Adobe JPEG Encoder class which can be found here: http://code.google.com/p/as3corelib/. You will also need some server-side action which in this case was done in PHP

  1. import com.adobe.images.JPGEncoder;
  2.  
  3. function createJPG(mc:MovieClip, n:Number, fileName:String) {
  4.  
  5. var jpgSource:BitmapData = new BitmapData (mc.width, mc.height);
  6. jpgSource.draw(mc);
  7. var jpgEncoder:JPGEncoder = new JPGEncoder(n);
  8. var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
  9.  
  10. var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
  11.  
  12. //Make sure to use the correct path to jpg_encoder_download.php
  13. var jpgURLRequest:URLRequest = new URLRequest ("download.php?name=" + fileName + ".jpg");
  14. jpgURLRequest.requestHeaders.push(header);
  15. jpgURLRequest.method = URLRequestMethod.POST;
  16. jpgURLRequest.data = jpgStream;
  17.  
  18. var loader:URLLoader = new URLLoader();
  19. navigateToURL(jpgURLRequest, "_blank");
  20. }
  21.  
  22.  
  23. //createJPG(movieClip, quality, fileName);
  24. createJPG(myMovieClip, 90, "myDog");
  25.  
  26. ///////////////////////////////////////////////////////////////
  27. //This is an Example of the Server-side that will return the JPEG
  28. ///////////////////////////////////////////////////////////////
  29. /*
  30. <?php
  31. if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
  32. // get bytearray
  33. $im = $GLOBALS["HTTP_RAW_POST_DATA"];
  34.  
  35. // add headers for download dialog-box
  36. header('Content-Type: image/jpeg');
  37. header("Content-Disposition: attachment; filename=".$_GET['name']);
  38. echo $im;
  39. } else echo 'An error occured.';
  40. ?>
  41. */

Report this snippet  

You need to login to post a comment.