4

I am taking a photograph from within my Android app using this code:

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
        photo = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] photoByte = baos.toByteArray();
        encodedImage = Base64.encodeToString(photoByte,Base64.DEFAULT);
    }

I am sending this String encodedImage to my PHP server through POST and receiving it to $encodedImage. I have a database where I have a field myImage of type MEDIUMBLOB. I tried to save the encodedimage in myImage but it is saved as corrupted image. I tried to save as base64_decode($encodedImage) but somehow that didn't work either.

I want three things to be done:

  • Save image to server-side database (BLOB)

  • Show image to a webpage

  • Send it back to the Android app.

I am facing issues in understanding the conversion of image required in different formats for the above tasks.

For my current project, I don't want to save my image to folder and give link to the database, so that option is closed for me.

My PHP code to save the image:

$baseImage = $_POST['baseImage'];
$blobImage = base64_decode($baseImage);
$query = "INSERT INTO `complaints`(`myImage`,...) VALUES ('$blobImage',...)"
$result = mysqli_query($conn,$query);
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Srujan Barai
  • 2,295
  • 4
  • 29
  • 52

2 Answers2

3

Do not decode your image in PHP, store it as you receive it, Now, in frontend you can print the image as follow

<?php
    $img = '<img src="data:image/jpeg;base64,' . $encodedImgStoredInDB . '">';
    print $img;
?>
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • You mean for the saving purpose I should remove the line `$blobImage = base64_decode($baseImage);` . Rest is ok? – Srujan Barai Mar 16 '16 at 14:00
  • @SrujanBarai yes :) have you tested it ? – Halayem Anis Mar 16 '16 at 14:16
  • I did save it that way. Then I went to my database. The myImage field showed me the image. I clicked to download it. But after saving it to computer it was blank image with extension `.bin` – Srujan Barai Mar 16 '16 at 14:19
  • @SrujanBarai we must keep SO clean, if you have new issue you should create a new thread and i'll be happy to help you :) – Halayem Anis Mar 16 '16 at 14:24
  • It isn't a new issue. It is just the same issue not solved this way. But I'm working on it. The bigger problem is, I'm new to php and sql, a little bit research and your hint should help. I'm working on it. – Srujan Barai Mar 16 '16 at 14:27
1

Prefix your SQL with the keyword _binary"

$query = "INSERT INTO `complaints`
(`myImage`,...)
VALUES (_binary'".addcslashes($blobImage, "\x00\'\"\r\n")."',...)"

Also, setup a test environment with CURL so that you can throw base64 encoded images at it repeatedly. This is going to be a case where you need lots of debugging.

<?php
$f = fopen('sample.jpg.base64.txt', 'w');
$i = fopen('sample.jpg', 'r');
if (!$i) { die("cannot open sample.jpg\n"); } 
$bytes = '';
while ( ! feof($i)) { 
    $bytes .= fread($i, 4096);
}
fclose($i);
fputs($f, base64_encode( $bytes ) );
fclose($f);

And then use curl to post it repeatedly and debug your PHP

curl -X PUT "http://localhost/myimport.php" -F "baseImage=@./sample.jpg.base64.txt"

Adjust your PHP script to simply write out the data to a file on your hard drive and inspect it there. Write out multiple versions, both encoded and decoded.

Personally, I wouldn't base64 the bytes going from Android to PHP, but I would base64 encode them in mysql.

chugadie
  • 2,786
  • 1
  • 24
  • 33
  • If you wouldnt base64 the bytes, how would you prefer to transfer then? It will be easy for me to do any changes on the Android side. Your answer is bit heavy for a new commer to php. :\ – Srujan Barai Mar 16 '16 at 14:52
  • Just send the raw bytes over http. You _can_ base64 encode, the only downside is increased payload size, not a serious issue. http://stackoverflow.com/questions/6360207/android-sending-a-byte-array-via-http-post – chugadie Mar 16 '16 at 15:09
  • Will it be more appropriate and easy to handle Bytes when received in php? – Srujan Barai Mar 16 '16 at 15:10
  • nope. You might access them as files from the $_FILES global array as if you had a web page submit an upload. Generally you're just getting some X% larger files transferred before limitations are met where X is the overhead of converting binary to base64. – chugadie Mar 16 '16 at 16:47
  • Just do both, if the array key $_POST['b64Image'] is set, then process the data as if it were base64 encoded, if $_POST['rawImage'] is set then treat those bytes as a raw JPEG. Then just send whichever you want from the client. – chugadie Mar 16 '16 at 16:49