I have from AS3 AIR App to send an image, beside rest of POST parameters to the PHP script which will do the rest. I want to somehow convert byte array with image to string and encode it with base64. I was successfull, but image data is wrong.
Here is the code I used to convert it:
...
//BA1 is Byte Array with an image in it
var data:String = BA1.toString();
OutSql.push({t: "b1", v: Base64.encode(data)});
...
Everything works fine, this data is sent to server, decoded, and stored as an image, but image is wrong. Somehow it is about 40 kb, while when I save it within Air application it is 22 kb. Any ideas?
p.s. I know that I can save it localy and upload it, but I really need to do it this way. Also, BA1.readUTF() generates an error, so not an option.
Addition:
On a server side I have tried to utf8_decode string before writing to file, and somehow I got an image which is proper dimensions, but... that image is not what I wanted to be, it looks like scribble...
import com.sociodox.utils.Base64;
.....
//BA1 is ByteArray with an image encoded
var enc_image=Base64.encode(BA1);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
var request:URLRequest = new URLRequest("some.php");
var variables:URLVariables = new URLVariables();
variables.decode("image="+enc_image);
request.method = URLRequestMethod.POST;
request.data = variables;
loader.load(request);
of course, set your listeners, too...
in "some.php":
$imageData = base64_decode(str_replace(" ", "+", $_POST['image']));
$fh = fopen("path/to/image/somename.jpg", "wb");
fwrite($fh, $imageData);
fclose($fh);
This works like a charm :)
Soultion found. I have downloaded from http://www.sociodox.com/base64.html Base64.swc which is actually encoding and decoding image Byte Arrays. And because my string was JSON-ed (as a part of object sent to PHP) I had only to convert spaces to '+' and decode it and write to file - works perfect! Case closed.
Related
I currently have a situation where FormData doesn't work for me. I'm using a specific "engine" Adobe CEP which is a little antiquated and the formData causes issues. My solution was to upload a file directly via base64 encoding. From Node JS I do:
let buff = new Buffer(filePath);
let base64data = buff.toString('base64');
console.log(base64data);
The console.log gives me the string:
Li4vLi4vLi4vLi4vLi4vLi4vLi4vVXNlcnMvb21hcmd1em1hbi8vRGVza3RvcC9Zb28vYWkyaHRtbC1vdXRwdXQvaG9tZS1wcm8tQXJ0Ym9hcmRfMS5qcGc=
axios.post(API_ENDPOINT, base64data)
.then(res => console.log(res))
From here I upload to a php server via axios.
The server receives it and I do:
<?php
$data = json_decode( file_get_contents('php://input') );
file_put_contents('testmeimg.jpg', base64_decode($data));
echo 'completed';
?>
another attempt
$data = file_get_contents('php://input');
file_put_contents('testmeimg.jpg', base64_decode($data));
var_dump($data);
"string(120) "Li4vLi4vLi4vLi4vLi4vLi4vLi4vVXNlcnMvb21hcmd1em1hbi8vRGVza3RvcC9Zb28vYWkyaHRtbC1vdXRwdXQvaG9tZS1wcm8tQXJ0Ym9hcmRfMS5qcGc="
"
The file correctly gets saved to my server but it's damaged and unreadable.
It also appears with zero bytes. Any idea what I'm doing wrong? or how I should handle this b64?
Thanks to the great help by #rickdenhaan aka the man.
I changed my node buffer code to:
let buff = new Buffer(fs.readFileSync(filePath));
let base64data = buff.toString('base64');
Previously I wasn't sending the file buffer only the path as a buffer.
Then on the php side this worked:
$data = file_get_contents('php://input');
file_put_contents('testmeimg.jpg', base64_decode($data));
I am trying to send a buffered video to save it as a file on my server.
Always the file is empty.
My js get the url "blob:https://..." and sends to the php that receive it.
$file = file_get_contents(url);
file_put_contents($video_url_mp4."helloWorld.webm",$file);
I tried send the video as canvas and i only get one frame.
suggest please
A blob url is only usable in the browser that created it. So you cant use it on your server or copy and paste it into another browser or send the link to your friend etc.
What you need to do is get the blob that url was created from and use a FormData object and upload that to your server.
I get a file with the content as string on my php but this is not in a video format.
my js:
var myFile = new File(video.src);
var fd = new FormData();
fd.append('data', myFile);
and my php:
$f = $_POST['data'];
$decode = base64_decode(preg_replace('/^data\:image\/webp\;base64\,/', '', $f));
what is wrong?
I need help in exporting a bytearray to a wav or mp3 (or anything that runs!) from my flash code to my php page.
I am using microphone recorder to record voice, i use a a url request to send my ByteArray to my PHP page.
i send the bye array from flash like this:
var url = "http://localhost/wordswesay/uploads/testrec.php";
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest(url);
request.requestHeaders.push (header);
request.method = URLRequestMethod.POST;
request.data = soundBytes; //FLV byteArray
var loader:URLLoader = new URLLoader();
trace(request.data);
loader.load(request)
and in my php file, i do the following:
<?php
echo "test";
$im = $GLOBALS["HTTP_RAW_POST_DATA"];
$fp = fopen("test.wav", 'w');
fwrite($fp, $im);
fclose($fp);
?>
at the server side (the sime file as the php script) i get the test.wav with about 200kb of size but i can not play it! media players says it can't play this file as the codec might not be supported.
Any help would be appreciated
There is more to a audio file than the audio bytes, there needs to be a format specific header, and the bytes need to be stored in a way specific to the format type, which is often (mp3s for example) different then the raw bytes Flash will be giving you (which are decoded already)
Take a look on Google for a audio encoder, either in AS3 or PHP that will convert your bytes to a mp3 file, I'm sure there is something, but I don't know one off hand. Hopefully that explains why its not working, and pushes you into the right direction. Goodluck!
Here's what I am trying to accomplish. I have a BitmapData that I am sending to a PHP function as ByteArray on the server by using URLRequest. The PHP function then saves the ByteArray as a jpg. All is well, so far and I am able to save the image on the server. Where I am stuck is that the PHP function generates the image file name randomly and I have no clue on how to get the name of the file I have just saved on the server. I am using the below code to send byteArray to the PHP function. How to modify/add this so I can get the filename of the image once it's saved on the server?
var myHeader:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
var myReqst:URLRequest = new URLRequest(path);
myReqst.requestHeaders.push(myHeader);
myReqst.method = URLRequestMethod.POST;
myReqst.data = imageBytes;
var myLoader:URLLoader;
myLoader = new URLLoader();
myLoader.load(sendReq);
If you're using URLLoader (which I normally don't, HTTPService is better IMO), you can listen for the 'complete' even to the triggered, which you can then check the 'data' property on URLLoader for the filename returned by the php function (as long as php actually returns it).
Im creating flash game that have the functionality to capture/record its gameplay that can be viewed later by the user, like a replay.
As for now Im already able to record the game and write it into a flv format in a ByteArray variable.
What Im working right now is how to send that ByteArray(the video file) to a php script and save it to a web server.
I've run into the URLLoader in flash where it can send the data and php will receive it thru the POST method. Unfortunately, the ByteArray containing the flv data must be first correclty encoded for php to read it. I figured this out from the example in the net where it does the same thing only that it is only sending a JPEG ByteArray instead of a FLV format using the JPEGENCODER.
Is there any existing class like the JpegEncoder for FLV format or any Video formats? Or any work around like creating the encoder my self?
Thanks in advance guys.
here is my current code to send the ByteArray
//send byteArray to a php script
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
//need to correctly encode first the ByteArray in FLV format
//....
request.data = fs; //FLV byteArray
var loader:URLLoader = new URLLoader();
trace(request.data);
Just figured this out.
I can now successfully send the flv byteArray in a php script where then the php receives it and save it as flv file in local system and uploading it in the youtube server using zend framework.
here's my code in flash.
//send byteArray to a php script
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest(url);
request.requestHeaders.push (header);
request.method = URLRequestMethod.POST;
request.data = fs; //FLV byteArray
var loader:URLLoader = new URLLoader();
trace(request.data);
loader.load(request)
and the scipt that receives and save it as a flv file:
$im = $GLOBALS["HTTP_RAW_POST_DATA"];
$fp = fopen("testVid.flv", 'w');
fwrite($fp, $im);
fclose($fp);
It turns out that I really dont need to encode the byteArray like what the JPEGEncoder is doing. Im still very new in flash and php though. And I have no idea if this is the best practice I can have. An advice would be greatly appreciated. Thanks guys.