将位图数据从 Flex 发送到 Php [英] Send bitmap data from Flex to Php

查看:28
本文介绍了将位图数据从 Flex 发送到 Php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网络服务器 (LAMP) 上保存来自我的 Flex 应用程序的屏幕截图.这是 Flex 代码:

I want to save a screenshot from my Flex app on the Webserver (LAMP). Here is the Flex code:

    private function getBitmapData( target : UIComponent ) : BitmapData
        {
            var bd : BitmapData = new BitmapData( target.width, target.height );
            var m : Matrix = new Matrix();
            bd.draw( target, m );
            return bd;
        }

现在,我如何向服务器发送/接收这些数据?

Now, how do I send / receive this data to the server?

推荐答案

您将不得不使用 HttpService 将数据发布到您网站上的页面.当我实现这个时,我将图像数据作为 Base64 编码的字符串发布到一个 PHP 页面,该页面使用 GD 库将其保存到服务器上的 png 文件中.这是我的代码的简化示例

You are going to have to use a HttpService to post the data to a page on your website. When I implemented this I posted the Image data as a Base64 encoded string to a PHP page that used the GD library to save it to a png file on the server. Here is a simplified example of what my code looked like

弹性代码

public function saveImg():void{
    var bd:BitmapData = new BitmapData(mycanvas.width,mycanvas.height);
    bd.draw(mycanvas);
    var ba:ByteArray = PNGEncoder.encode(bd);
    var encoded:String = Base64.encodeByteArray(ba);
    var objSend:Object = new Object;
    objSend.data = encoded;
    objSend.filename = _imgResult;

    writeImage.send(objSend);
 }

<mx:HTTPService id="writeImage" url="/saveImage.php" method="POST" resultFormat="text" result="resultHandler(event)"/>

PHP 文件 (saveImage.php)

PHP File (saveImage.php)

<?php
//check for the posted data and decode it
if (isset($_POST["data"]) && ($_POST["data"] !="")){
    $data = $_POST["data"];
    $data = base64_decode($data);
    $im = imagecreatefromstring($data);
}
//make a file name
$filename = "test"

//save the image to the disk
if (isset($im) && $im != false) {
    $imgFile = "/etc/www/html/".$filename.".png";

    //delete the file if it already exists
    if(file_exists($imgFile)){
        unlink($imgFile);      
    }

    $result = imagepng($im, $imgFile);
    imagedestroy($im);
    echo "/".$filename.".png";
}
else {
    echo 'Error';
}
?>

在 flex 方面,我使用的是来自 dynamicflash 的 Base64Encode 实用程序,但是现在 flex 内置了一个,您可以使用它来代替.在您的 php 配置中,您需要确保启用了 GD 库,以便您可以保存图像.

On the flex side I am using the Base64Encode utilty from dynamicflash, but now that there is one built into flex you could use that instead. In your php config you will need to make sure you have the GD library enabled so that you can save the image.

当然,这是一个非常简单的示例,并没有考虑到所需的所有错误处理和安全问题,但应该为您提供一个良好的基础.

Of course this is a very simple example and does not take into account all the error handling and security concerns needed, but should provide you a good base to get going with.

这篇关于将位图数据从 Flex 发送到 Php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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