Displaying a Base64 Images from a Database Via PHP

Displaying a Base64 images from a database via PHP

The solution to your problem is here:

How to decode a base64 string (gif) into image in PHP / HTML

Quoting that source but modifying:

In the case you strip out the first case and choose to decode the string, you should add this before echoing the decoded image data:

header("Content-type: image/gif");
$data = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo base64_decode($data);

In the second case, use this instead:

echo '<img src="data:image/gif;base64,' . $data . '" />';

The second case is bad because the browser does not perform caching if the same image is shown on multiple pages.

What is the correct way of displaying base64 image from database to php page?

I would store the image directly encoded in base64 an also its extension for proper display.

The content field has to be TEXT type in your database, otherwise you won't store the data properly and you won't be able to get it as you should


$dsn = 'mysql:host=localhost;dbname=website;charset=utf8mb4';
$pdo = new PDO($dsn, 'root', '');

if (isset($_POST['upload'])) {
// Get the image and convert into string
$img = file_get_contents($_FILES['image']['tmp_name']);

// Encode the image string data into base64
$content = base64_encode($img);
// Get the extension
$extension = strtolower(end(explode('.', $_FILES['image']['name'])));
if (!empty($content)) {
$sql = " INSERT INTO images(content, extension) VALUES (:content, :extension) ";
$pdo->prepare($sql)->execute(array('content' => $content, 'extension' => $extension));

// Later in your code
<div class="images" id="Images"></div>
$sql = " SELECT * FROM images ";
$result = $pdo->query($sql);
if ($result->rowCount() > 0) {
while ($row = $result->fetch()) {
echo "<img src='data:image/{$row['extension']};charset=utf-8;base64,{$row['content']}' alt='Binary Image'/>";

How to fetch images from database in base64_encode in php

You will need to change the following:

<img src="data:image/jpg;base64,'.base64_encode($row['images']).'"  />


<img src="data:image/jpg;base64, <?php base64_encode($row['images']);?>"  />

Trying to display image encoded in base64

Seems you're encoding the filename in database and not the file contents.

Read about http://php.net/manual/pt_BR/function.file-get-contents.php

'image' => base64_encode(file_get_contents($fileName)),

insert base64 image to a mysql table in php

In general you don't want to store images in a relational database.

But as you want.

here's the code to store a blob using MySQLi(mysqli manual):

first: Create a table to store your images.

`id` int(11) NOT NULL,
`content` blob NOT NULL


$host = 'localhost';
$user = 'root';
$pass = 'root';
$db_name = 'test';

$base64_data = $_POST['base64_data'];

$link = mysqli_connect($host, $user, $pass, $db_name);

$stmt = mysqli_prepare($link, "INSERT INTO test (id, content) VALUES (?, ?)");

$stmt->bind_param('ib', $id, $content);

$id = 1;

$content = null;

//$base64_data = base64_decode(base64_encode(file_get_contents('3413983627135443.jpg')));
//$stmt->send_long_data(1, $base64_data);
$stmt->send_long_data(1, base64_decode($base64_data));

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

Do not use the MySQL (Original)Extensions : intro.mysql

Related Topics

Leave a reply