Standara way for passing image from android to php server - php

I'm trying to pass an image from Android application to php server.
I searched about that, I have two ways:
Sending by Multipart
Encode to Base64
I'm using Volley, I know about both but I don't know which is better for implementation.
Can you tell me which is standard and better?

Do not go with the base64 string since it may give you crash if it exceeds the limit. Better option to use the Multipart, it is reliable and safe.

Related

AS3 BitmapData source code

A mobile AIR app needs to send a large image to the back end for later display in a web page.
It takes for ever for PNGEncoder in the AIR app to complete, so the idea is to convert the image data to a ByteArray, compress it and send it to the PHP backend where it is saved as PNG by the PHP code. So I'm looking to port PNGEncoder.as and BitmapData class to PHP to accomplish this. I found PNGEncoder.as in as3corelib but can't find source for BitmapData class that it uses.
So the questions are
1. Is there code out there that does what I'm trying to do?
2. Where can I find BitmapData source code?
3. Is there another way to accomplish this that I'm missing?
Note1. I realize that I can decompile airglobal.swf where BitmapData resides, but looking for a cleaner way
Note2. I'm aware of AMFPHP but it does not support BitmapData type
Thanks
Andy
I see two solutions to this issue.
First, you can consider using a Worker to do just the conversion and sending routine, because image conversion is a pretty standalone and straightforward task to offload, and since most modern devices have more than one core anyway, it's better to put the hardware to work on the client rather than on server. Of course, you will have to take some measures to provide the worker instance with required data and to properly upload the image (cookies might not be handled well in a worker), but this approach is generally cleaner and requires no server side alterations.
The second approach is to use BitmapData.getPixels() to convert a region of pixels into a sequence of bytes, then send them unaltered to the server for conversion. Be warned however, the amount of data in a raw bitmap can be too large for the server to accept, you are looking to no less than 4 bytes per pixel, as bitmaps in AS3 are 32-bit. You can use a server side image encoder to convert raw data on the server after uploading.

createimagefromstring gives invalid chunk type

Did not find any solutions to my problem .. so here we go..
I have a SLIM webservice, which supports uploading a report containing one or more base64 encoded images.
All data i receive in my webservice functions are json encoded and therefore i decode everything before handling the data.
I tested my function using Advanced Rest Client, and everything worked well, meaning that the images were created successfully.
After integrating the webservice with my iPhone app i got a problem though. The function createimagefromstring returns: invalid chunk type. Even tried to generate new base64 encoded image data using a website for that purpose, and still gave me the same error. It seems that im only able to use the base64 encoded image data that use in Advanced Rest Client.
Additional info: The image i used with success is a very small image, and thus the base64 string is not big. Tried using another image (slightly biggeR) and that worked too. But bigger images doesnt seem to work.. could there be problems here ?
Desperate for help, as the integration between the iPhone app and the webservice must be done soon :)
Thanks in advance
Digging some more revealed that the imagecreatefromstring was not the source of failure.
Strange Base64 encode/decode problem
seems like the encoding removes the + characters and replaces them with space instead.
Solution to my problem so far is:
str_replace(' ','+',$image);
and then afterwards do the base64_decode.
Should anybody have a better solution, please let me know..

Recording Audio on website: Red5 stream or posting the audio data?

Let me first establish what I want to do:
My user is able to record voicenotes on my website, add tags to said notes for indexing as well as a title. When the note is saved I save the path of the note along with the other info in my DB.
Now, I have 2 choices to do the recording, both involve a .swf embedded in my site:
1) I could use Red5 server to stream the audio to my server and save the file and return the path to said file to my app to do the DB saving, seems rather complicated since I would have to convert the audio and move it to the appropriate folder that belongs to the user in a server side Red5 app, which I'm not very aware of how to build.
2) I could simply record the audio and grab its byte array, do a Base64 encoding on it and send it to PHP along with the rest of the data that is necessary (be it by a simple POST or an AJAX call), decode it on the server and make the file with the appropriate extension, audio conversion would also occur here using ffmpeg, this option seems simpler but I do not know how viable it is.
What option would you say is more viable and easier to develop? Thanks in advance
Depending on the planned duration of the recording, you may very well be able to use option number two. I recently used a similar approach successfully for a project, but recordings were only up to 30 seconds or so. Here's what I did differently from what you're suggesting though, and why I think it's better:
To capture the sound from the microphone and store it to a ByteArray, use the SAMPLE_DATA event which is dispatched whenever more sound data comes in from the microphone. There's an example in the documentation that should explain this well enough.
Because most users would be on normal home computers without any special recording equipment, it was safe to assume that the full fidelity of the recording is not necessary. I used just 2 bytes per sample, and only mono, instead of using the full 64 bit floats (AS3 Number) that you get from the microphone on the SAMPLE_DATA event. Simply read the Number and do myFloatSample * 0x7fff to convert to 16 bit signed integer.
Don't use the native 44.1kHz sampling rate if you're just recording speech or something else in that frequency range. You will likely get away just fine with 22.05kHz, which will cut the amount of data in half straight away. Just set the Microphone.rate property accordingly.
Don't use Base64 to encode your data. Send it as binary data, which will be significantly smaller. You can send it as raw POST data, or using something like AMF. Also, before you send it, use the native compress() or deflate() methods on the ByteArray to compress it. On the server, decompress using the ZLIB or raw DEFLATE (inflate) algorithms respectively, which PHP supports.
Once decompressed on the server, what you have is essentially what is called a raw 16-bit mono PCM stream. Incidentally, that should be one of the very input formats that ffmpeg (or lame) supports, so you should be able to encode it to mp3 without having to do any manual decoding first.
Obviously the Red5 solution will likely be better, because it's more tailored for the task. But if you don't have the resources to set up a Red5 server, or don't want to use Java, the above solution is proven to work well as long as you stay away from too long recordings.
To take a simple example, a 30 second recording at 22,050 samples per second, 2 bytes per sample will be ~1.3MB. Even once deflated, the transfer to the server will likely still be almost a megabyte for 30 seconds of audio. This may or may not be acceptable for your application.

Send an Actionscript ByteArray as a POST variable (within Facebook)

I would like to use Flash to send a ByteArray (of a PNG image) to a php file, in a facebook application. Is there a way to do this by sending the ByteArray as just one POST variable instead of as the entirety of the POST data?
There was a nearly identical question here: How can I send a ByteArray (from Flash) and some form data to php? but the problem is different; instead of smuggling other variables in other parts of the request, the image itself has to be sent as just a variable because Facebook commandeers the post data and puts in its own junk.
Is this at all possible? If not, can I send the image in some form other than a byteArray?
I think the easiest way is base64 encoding the image before sending it. Then it's just a string and it's safe to pass it as a regular POST variable.
On the php side, you just have to base64_decode this string and then you have your image data ready to save it to file or whatever you need (you could also feed it to GD or other such library if you need to manipulate it first).
Another option, at least in theory is using multipart/data, just like you'd use in an html form to send a file, but if I recall correctly, the player does not allow to send files using this method (until version 9.0.124 or something like that, this was possible).
So, base64 is easy and simple and it only adds some overhead; 1/3 of the file payload in terms of size and some processing time as weel, but in most cases this isn't a big deal.

Image uploading using another site's API - Bypassing my server

I want to use imgur's API to upload images however I don't want to use my server too much in the act because I'd like to keep bandwidth to a minimum.
The API accepts images in a base64 encoded string or binary.
Also if there's a way to encode images into base64 or binary in javascript that would be really useful to know too
JavaScript security restrictions prevent you from reading a local file and doing anything with it. That will be the reason that you cannot encode a file in Base64 and send it directly to that API.
Does Imgur have an API which accepts a direct file upload?

Categories