Send bitmap data from Flex to Php

I want to save a screenshot from my Flex application on a web server (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?

+3
source share
1 answer

You will need to use the HttpService to place the data on the page of your website. When I did this, I sent 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 how my code looked

Flex Code

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 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';
}
?>

Base64Encode dynamicflash, , flex, . php config , GD, .

, , .

+5

Source: https://habr.com/ru/post/1758168/


All Articles