来自 Flash 的位图数据 -> PHP->电子邮件

发布于 2024-10-26 04:24:22 字数 2525 浏览 1 评论 0原文

我正在尝试在 Flash 中截取影片剪辑的“屏幕截图”,使用 AS Core Lib JPGEncoder 类将其编码为 Jpg,然后将生成的 ByteArray POST 提交到 PHP,并将图像嵌入 MIME 编码的电子邮件中。

目前,我已经从 Flash 保存了编码的 ByteArray,并且工作正常,因此问题出在从 Flash 发送到 PHP 的过程中。 我正在使用 SwiftMailer 发送一封复杂的电子邮件,并以 jpeg 作为附件。目前,在我根据发送的数据构建附件时,脚本似乎崩溃了。

这是动作脚本:

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");
    }

这是 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('[email protected]'=>'Battle for Brisbane'))  
    /*Set the to addresses with an associative array*/  
    ->setTo(array('[email protected]'))  
    /*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); 

这是一项专业工作,因此我们将不胜感激。

更新:这不是 SwiftAttachment,而是 Swift_Attachment。缺少下划线,问题已解决,应用程序功能正常。感谢所有发帖帮助我的人

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 subject')  
    /*Set the from address with an associative array*/  
    ->setFrom(array('[email protected]'=>'Battle for Brisbane'))  
    /*Set the to addresses with an associative array*/  
    ->setTo(array('[email protected]'))  
    /*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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-11-02 04:24:22

您是否已验证编码图像是否有效?

如果您有本地服务器或不同的服务器,请在那里尝试。

此外,您可以尝试将一张图像加载到闪存中并将其发送到服务器,看看是否有效,而不是发送生成的图像。

至于如何从flash发送图像,试试这个

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) 
}

对于php脚本,你可以先尝试保存文件。

//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 );

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 );
纵性 2024-11-02 04:24:22

似乎问题一直是我在 Swift_Attachment 中缺少下划线。你不就是讨厌这样吗?

Seems that the issue all along was that I was missing an underscore in Swift_Attachment. Don't you just hate that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文