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 the resulting ByteArray to PHP, and embed the image in a MIME encoded email.
Currently, I've saved the encoded ByteArray from Flash, and that works fine, so the issue is in the sending from Flash to PHP. I'm using SwiftMailer to send a complex email with the jpeg as an attachment. Currently, the script seems to be crashing at the point where I build the attachment from the sent data.
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 subje开发者_运维百科ct')
/*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);
This is for a professional job, so any help would be greatly appreciated.
Update: It's not SwiftAttachment, it's Swift_Attachment. Missing underscore, problem solved, application functional. Thanks to all who posted to help me with this
Have you verified that the encoded image is a valid one?
If you have a local server or a different server try it there.
Also, you can try to load one image into your flash and send that to the server and see if that works, instead of sending a generated image.
As for how to send the image from flash, try this
var request:URLRequest = new URLRequest( mailLoc );
request.contentType = 'application/octet-stream';
request.method = URLRequestMethod.POST;
request.data = bArray;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, MailCompleteHandler);
loader.addEventListener( IOErrorEvent.IO_ERROR, _onImageError );
try
{
loader.load(request);
}
catch(error:Error)
{
trace("Unable to load URL");
}
private function _onImageError(e:IOErrorEvent):void {
trace("IOErrorEvent: ",e.type," : ",e.text)
}
And for the php script, you can try first to save the file.
//bindary data.
$image_bytes = $GLOBALS["HTTP_RAW_POST_DATA"];
//change to whatever works for you
$file_name = "testfile.jpg";
$file_path = "../uploads/$file_name";
$file = fopen( $file_path, 'w+' );
if ( !fwrite( $file, $image_bytes ) ) {
return "Error writing to file: $file";
}
fclose( $file );
Seems that the issue all along was that I was missing an underscore in Swift_Attachment. Don't you just hate that?
精彩评论