I saved image in sql table column as blob. I want to load the blob image to html img tag in js using document.querySelector()
and URL.createObjectURL(image)
. If using php, we need to declare src=<?php echo $encode_img; ?>
in img tag. But, I do not want to declare this way. My code in js could not load image successfully.
Reference: using-php-to-display-blob and using-javascript-to-display-a-blob
load blob image from sql in html using php-worked
<?php
// img saved as blob from sql
$image =$array_image[0]['file'];
$encode_img ='"data:image/jpeg;base64,'.base64_encode($image).'"';
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<img id="i" src=<?php echo $encode_img; ?> alt="Test">
</body>
</html>
load blob image from sql in html using js(URL.createObjectUrl)- not worked
<?php
// img saved as blob from sql
$image =$array_image[0]['file'];
?>
<!DOCTYPE html>
<html>
<head>
<script src="../../library/jquery/jquery-3.4.1.min.js"></script>
<script>
$( document ).ready(function()
{
image=<?php echo $image; ?>;
html_i=document.querySelector("#i");
var objectURL = URL.createObjectURL(image);
html_i.src = objectURL;
});
</script>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<img id="i" alt="Test">
</body>
</html>
load blob image from sql in html using js(without URL.createObjectUrl)- worked
<?php
// img saved as blob from sql
$image =$array_image[0]['file'];
$encode_img ='"data:image/jpeg;base64,'.base64_encode($image).'"';
?>
<!DOCTYPE html>
<html>
<head>
<script src="../../library/jquery/jquery-3.4.1.min.js"></script>
<script>
$( document ).ready(function()
{
html_i =document.querySelector("#i");
html_i.src =<?php echo $encode_img; ?>;
});
</script>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<img id="i" alt="Test">
</body>
</html>
Thanks in advance.