首页 > 解决方案 > 通过 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-blobusing-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>

提前致谢。

标签: javascripthtmlimageblob

解决方案


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


推荐阅读