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.
IMPORTANT
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
<?php
$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>
<?php
$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'/>";
}
}
?>
</div>
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']).'" />
to
<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.
CREATE TABLE `test` (
`id` int(11) NOT NULL,
`content` blob NOT NULL
)
then:
<?php
$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));
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
mysqli_stmt_close($stmt);
Do not use the MySQL (Original)Extensions : intro.mysql
Related Topics
How to Change the Background Color of Dropdown List in Select Tag
Sending Message in Telegram Bot With Images
Convert Svg Image to Png With PHP
How to Show Blob Image in HTML
Creating Advanced Search Query With PHP Mysqli
On a Function That Gets Settings from a Db I Ran into the Error
How to Convert Ereg Expressions to Preg in PHP
Should We Ever Check For MySQLi_Connect() Errors Manually
How to Get Time Difference in Minutes in PHP
Send Python Output to HTML Field
How to Download an Mp3 File from Remote Url
[Php][Mysql] How to Insert Data in 2 Tables At Same Time
A Way to Determine Browser Width in PHP Without JavaScript
Add Space After Every 4Th Character
Success Message Pop-Up After from Submit Button Clicked
Undefined Index: User_Id. Where, User_Id Is Session Variable