从Flash到PHP到电子邮件的BitmapData

UPDATED: SEE BOTTOM OF MESSAGE

Howdy All, Here's the issue: I'm trying to take a 'screenshot' of a movieclip in Flash, encode it as a Jpg using the AS Core Lib JPGEncoder class, then POST submit it to PHP, and embed the image in a MIME encoded email.

Currently, I've tested saving the encoded image locally, and that works, so the encoder is definitely working. The email sends, and it has a 100kb jpg attachment as it should, however, the image appears to contain bad data, as it won't open properly in any application.

Here's the Actionscript:

trace("Sending Email");
    var rootMC:MovieClip = MovieClip(root);
    var data1:BitmapData = new BitmapData(rootMC.width, rootMC.height); 
    data1.draw(rootMC);

    var en:JPGEncoder = new JPGEncoder(80);
    var bArray:ByteArray=   en.encode(data1);

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

    var request:URLRequest = new URLRequest();
    request.requestHeaders.push(header);
    request.url = mailLoc;//MailLoc is the URL of the PHP.
    request.method = URLRequestMethod.POST;
    request.data = bArray;
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    loader.addEventListener(Event.COMPLETE, MailCompleteHandler);
    try
    {
        loader.load(request);
    }
    catch(error:Error)
    {
        trace("Unable to load URL");
    }

And here is the PHP:

require_once 'lib/swift_required.php';  
$image = file_get_contents("php://input");  
$attachment = SwiftAttachment::newInstance($image, 'submission.jpg', 'image/jpg');//<--This line stuffs it  

$message = Swift_Message::newInstance()  
    /*Give the message a subject*/  
    ->setSubject('Your subject')  
    /*Set the from address with an associative array*/  
    ->setFrom(array('info@battleforbrisbane.com.au'=>'Battle for Brisbane'))  
    /*Set the to addresses with an associative array*/  
    ->setTo(array('jordaanm@gmail.com'))  
    /*Give it a body*/  
    ->setBody('Congratulations! You submission to Battle for Brisbane was received'); 
    $message->attach($attachment);//<--When the attachment above is commented out, so is this  

    $transport = Swift_SendmailTransport::newInstance();  
    $mailer = Swift_Mailer::newInstance($transport);  
    $mailer->send($message); 

Update: I'm now using SwiftMailer instead of manually writing out the MIME. However, here is the new deal: In the php code, I've marked a line that attaches the POSTed image data as a jpg to the email. If I comment out this line, and the message->attach line, then every sends fine. If they're uncommented, though, then no email is sent, which leads me to believe that trying to create a jpg attachment from the data supplied is causing the problem. This all just confirms my suspicions that the data being received by the PHP script is not correct. How nice and frustrating.

You should seriously consider using a modern mail library like SwiftMailer instead of building your own MIME headers and bodies. As you are now aware, doing this correctly by hand can be a royal pain.

You should also make sure that the data PHP is receiving is actually a bad image before assuming it's the mail that's going wrong. Try saving the results of your file_get_contents to disk and viewing it in your browser, just to make sure.

I'm slightly outside my comfort zone here, but I believe you may need to add some more header information.

Jonathan Marston has created a wrapper class to do this, which can be found here: http://code.google.com/p/asfeedback/source/browse/trunk/com/marston/utils/URLRequestWrapper.as?spec=svn4&r=4#

Which you could try using in the following way:

var byteArray : ByteArray = new JPGEncoder( 80 ).encode( data1);
var wrapper = new URLRequestWrapper(byteArray, "submission.jpg");
wrapper.url = mailLoc;

var ldr:URLLoader = new URLLoader();
ldr.dataFormat = URLLoaderDataFormat.BINARY;
ldr.addEventListener(Event.COMPLETE, MailCompleteHandler);
ldr.load(wrapper.request);