【问题标题】:Save image from Flash, send it to PHP and return a URL string to Flash从 Flash 保存图像,将其发送到 PHP 并将 URL 字符串返回给 Flash
【发布时间】:2011-08-29 13:41:39
【问题描述】:

我使用此代码将图像转换为 BitmapData 并将 JPG 存储在 ByteArray 中。

import com.adobe.images.JPGEncoder;

var jpgSource:BitmapData = new BitmapData (img_mc.width, img_mc.height);
jpgSource.draw(img_mc);

var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

// here we need some code to send the bytearray but I lack enough knowledge to do it by myself

现在,我想做以下事情: 1. 将 ByteArray 发送到 PHP; 2. PHP必须在服务器上存储一个物理的image_id.jpg; 3. 那么PHP必须将图片的URL返回给Flash;

这可能吗?怎么样?

PHP 的第一行可能是:

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
    // get bytearray
    $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];

    // but I don't know how to save the image on disk and how to return the URL of the //image
}

谢谢!

【问题讨论】:

    标签: php flash


    【解决方案1】:

    as3 部分:

    
    import com.adobe.images.JPGEncoder;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequestHeader;
    import flash.net.URLRequest;
    
    

    var jpgSource:BitmapData = new BitmapData(img_mc.width,img_mc.height); jpgSource.draw(img_mc); var jpgEncoder:JPGEncoder = new JPGEncoder(85); var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

    //set the request's header,method and data var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream"); var loader:URLLoader = new URLLoader(); //sends jpg bytes to saveJPG.php script var myRequest:URLRequest = new URLRequest("saveJPG.php"); myRequest.requestHeaders.push(header); myRequest.method = URLRequestMethod.POST; myRequest.data = jpgStream; loader.load(myRequest); //fire complete event; loader.addEventListener(Event.COMPLETE,saved); function saved(e:Event) { //trace the image file name trace(loader.data); }

    php (saveJPG.php) 部分:

    
    if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
    
    
    //the image file name   
    $fileName = 'img.jpg';
    
    // get the binary stream
    $im = $GLOBALS["HTTP_RAW_POST_DATA"];
    
    //write it
    $fp = fopen($fileName, 'wb');
    fwrite($fp, $im);
    fclose($fp);
    
    //echo the fileName;
    echo $fileName;
    

    } else echo 'result=An error occured.';

    【讨论】:

    • 查看stackoverflow.com/questions/2731297/… 以获得首选方式(使用 php://input 包装器)而不是 HTTP_RAW_POST_DATA。
    • 像这样以二进制格式发送请求不是更好吗? loader.dataFormat = URLLoaderDataFormat.BINARY;
    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2011-09-23
    • 2015-10-27
    相关资源
    最近更新 更多