我想在Web服务器(LAMP)上保存我的Flex应用程序的屏幕截图。 这是Flex代码:
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;
}
现在,如何将此数据发送/接收到服务器?
答案 0 :(得分:5)
您将不得不使用HttpService将数据发布到您网站上的页面。当我实现这个时,我将Image数据作为Base64编码的字符串发布到PHP页面,该页面使用GD库将其保存到服务器上的png文件中。这是我的代码看起来像
的简化示例Flex代码
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
//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库,以便保存图像。
当然,这是一个非常简单的示例,并未考虑所需的所有错误处理和安全问题,但应为您提供良好的基础。