Save base64encoded image to server database as blob

I am taking a photo from my Android application 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 send this line encodedImageto my PHP server via POST and get it $encodedImage. I have a database where I have a myImagetype field MEDIUMBLOB. I tried to save encodedImageto myImage, but it is saved as a damaged image. I tried to save how base64_decode($encodedImage), but somehow it didn't work.

I want to do three things:

  • Save Image to Server Database (BLOB)

  • Show image on web page

  • Send it back to the Android app.

, .

, .

PHP- :

$baseImage = $_POST['baseImage'];
$blobImage = base64_decode($baseImage);
$query = "INSERT INTO `complaints`(`myImage`,...) VALUES ('$blobImage',...)"
$result = mysqli_query($conn,$query);
+4
2

SQL _binary "

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

, CURL, base64. , .

<?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);

curl, PHP

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

PHP script, . , , .

base64 , Android PHP, base64 mysql.

+1

PHP, , , , , ,

<?php
    $img = '<img src="data:image/jpeg;base64,' . $encodedImgStoredInDB . '">';
    print $img;
?>
+3

All Articles