从Flash到PHP到电子邮件的BitmapData [英] BitmapData from Flash to PHP to Email

查看:149
本文介绍了从Flash到PHP到电子邮件的BitmapData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AS Core Lib JPGEncoder类将其编码为Jpg,然后将POST提交给PHP,并将图像嵌入到MIME编码的电子邮件中。



目前,我已经测试过在本地保存编码的图像,并且工作,所以编码器肯定是工作的。电子邮件发送,它有一个100kb的jpg附件,因为它应该,但是,图像似乎包含错误的数据,因为它不会在任何应用程序中正常打开。



  trace(发送电子邮件); 
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是PHP的URL。
request.method = URLRequestMethod.POST;
request.data = bArray;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE,MailCompleteHandler);
尝试
{
loader.load(request);

catch(error:Error)
{
trace(Unable to load URL);



这里是PHP:

  require_once'lib / swift_required.php'; 
$ image = file_get_contents(php:// input);
$ attachment = SwiftAttachment :: newInstance($ image,'submission.jpg','image / jpg'); //< - 这行代码

$ message = Swift_Message :: newInstance()
/ *给消息一个主题* /
- > setSubject('Your subject')
/ *设置关联数组的地址* /
- > setFrom(array('info@battleforbrisbane.com.au'=>'为布里斯班战役'))
/ *设置与关联数组的地址* /
- > ; setTo(array('jordaanm@gmail.com'))
/ *给它一个body * /
- > setBody('恭喜!
$ message-> attach($ attachment); //< - 当上面的附件被注释掉时,这个

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

更新:我现在使用 SwiftMailer 而不是手工写出MIME。但是,这里是新的协议:在PHP代码中,我已经标记了一个将张贴的图像数据作为jpg附加到电子邮件的行。如果我注释掉这一行,并且消息 - >附加行,那么每个发送罚款。如果他们没有注释,那么没有电子邮件发送,这导致我相信试图从提供的数据创建一个jpg附件导致的问题。
这一切都证实了我对PHP脚本收到的数据不正确的怀疑。多么好的和令人沮丧的。

解决方案

你应该认真考虑使用像SwiftMailer这样的现代邮件库,而不是构建自己的MIME头文件和主体。正如你现在意识到的那样,手工操作是正确的。



你也应该确保PHP正在接收的数据实际上是一个坏的图像之前,假设它是错误的邮件。尝试将您的 file_get_contents 的结果保存到磁盘并在浏览器中查看,以确保。


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.

这篇关于从Flash到PHP到电子邮件的BitmapData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆