Reputation: 39
I'm creating an android app as well as a website using PHP. I store encoding image from android app to db. That same image I want to show in website. I tried a lot but not showing that image in website. How to decode that same image using php and shown in website.
This method, I used to encode Image from App:
private String encodeTobase64(Bitmap image) {
Bitmap bitmap_image = image;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap_image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] b = stream.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
This line to show image in PHP:
$query = mysql_query("select * from ACCOUNT where (company_email='$email')") or die(mysql_error());
$value = mysql_fetch_array($query);
$image = $value['PROFILE_PICTURE'];
echo '<img class="img-circle" src="data:image/jpeg;base64,' . base64_encode($image) . '" width="40" height="40"/>';
Please, anyone help me! Thanks in advance...
Upvotes: 0
Views: 2929
Reputation: 1297
Storing base 64 string into the database is not good practice.
First, convert your base 64 string into image and store image path in the database.
You can decode base 64 string into image with the help of following function:
function convert_base64_to_image($base64, $image_path) {
$file = fopen($image_path, "wb");
$image_raw_data = explode(',', $base64);
// get decodable part of image
fwrite($file, base64_decode($image_raw_data[1]));
fclose($file);
return $image_path;
}
Now store image path in database and you can use this path directly to display your image.
echo '<img class="img-circle" src="$image_path" width="40" height="40"/>';
Upvotes: 1