javascript - 通过 URL.createObjectURL(image) 使用 js 将图像从 sql 表加载到 html img
问题描述
我将 sql 表列中的图像保存为 blob。我想使用document.querySelector()
and将 blob 图像加载到 js 中的 html img 标签URL.createObjectURL(image)
。如果使用 php,我们需要src=<?php echo $encode_img; ?>
在 img 标签中声明。但是,我不想这样声明。我在 js 中的代码无法成功加载图像。
参考:using-php-to-display-blob和using-javascript-to-display-a-blob
使用 php-worked 从 html 中的 sql 加载 blob 图像
<?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>
使用js(URL.createObjectUrl)从html中的sql加载blob图像-不起作用
<?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>
使用js(没有URL.createObjectUrl)从html中的sql加载blob图像-工作
<?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>
提前致谢。
解决方案
I retrieved blob image from sql in php. Then , I encode the blob using base64_encode
. This is beacause, I can get readable format(content without any symbols) of blob in javascript. I access encoded blob in javascript to create blob by b64toBlob
conversion and create bloburl using this blob. Then, I pass bloburl to image source.
<?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>
const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
$( document ).ready(function()
{
const contentType ='image/jpeg';
const b64Data ='<?php echo base64_encode($image);?>';
const blob =b64toBlob(b64Data, contentType);
const blobUrl =URL.createObjectURL(blob);
//console.log(blobUrl);
html_i =document.querySelector("#i");
html_i.src =blobUrl;
})
</script>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<img id="i" alt="Test">
</body>
</html>
b64toBlob conversion includes atob function
, .charCodeAt method
, Uint8Array constructor
, and Blob constructor
.I get b64toBlob
function from link in reference and you can find explanation of this function there.
Reference:creating-a-blob-from-a-base64-string-in-javascript
推荐阅读
- javascript - jQuery.when 是否接受本机 Promise 对象?
- database - How to open mongo-db dump file?
- ios - iOS 12 上的 CIDepthBlurEffect 渲染问题
- reactjs - 尝试在 React Js 中执行第二次数据获取时,为什么我得到无法访问的代码?
- r - 将列表值存储在具有不相等行的 data.frame 中
- stata - 如何格式化图例中的数字标签?
- tornadofx - 删除项目上的自定义单元格格式 ListView TornadoFx
- c# - 您如何选择 C# 中命名空间和类的命名约定?
- dagger-2 - 在匕首 2 中创建依赖组件
- android - 使用TextInputLayout时如何更改edittext的提示文本颜色