【问题标题】:BitmapData from Flash -> PHP -> Email来自 Flash 的 BitmapData -> PHP -> 电子邮件
【发布时间】:2011-07-21 20:04:49
【问题描述】:

我正在尝试在 Flash 中拍摄电影剪辑的“屏幕截图”,使用 AS Core Lib JPGEncoder 类将其编码为 Jpg,然后 POST 将生成的 ByteArray 提交给 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('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); 

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

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

【问题讨论】:

  • 这似乎是an exact duplicate of your question yesterday。我仍然希望看到 1x1 GIF 的原始输出,虽然在那边,而不是在这里。
  • 这是昨天问题的精炼版本。一些小问题由您自己解决(现在使用 SwiftMailer 而不是手动编码)。然而,大问题还没有结束。就 1x1 像素而言,我从 Flash->PHP 发送的任何数据都拒绝写入 (fwrite) 或 mime attach。
  • $imagestrlen 吗?

标签: php flash mime


【解决方案1】:

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

如果您有本地服务器或其他服务器,请在那里尝试。

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

至于如何从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 );

【讨论】:

  • 感谢那个 goliatone。我做了你建议的改变。 Flash 端没有针对 IO 问题提出任何标志。出于某种原因,PHP 脚本不写入文件。奇怪...
  • 您是否尝试过不同的 PHP 设置?还是发送您之前上传的图片而不是生成的图片?
【解决方案2】:

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

【讨论】:

  • 对,这很糟糕。但是使用适当的“调试”方法很容易隔离有问题的代码。基本上,把整个事情分解成更小的任务,测试一个,然后在上面构建下一个。当当前任务失败时,您知道可以专注于哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多