Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 一个数据库中有多个base64文件_Html_Image_Base64 - Fatal编程技术网

Html 一个数据库中有多个base64文件

Html 一个数据库中有多个base64文件,html,image,base64,Html,Image,Base64,您能否将这两个图像合并到上的一个外部文件中,并作为图像链接到它们 差不多 <img src="base64.html#img1" /> <img src="base64.html#img2" /> 数据URI(此处的键)通常是字符串,编码为base-64 因为它们是字符串,所以可以将它们连接为任何其他字符串。但是,不能将串联(数据uri)字符串直接设置为图像源 连接时,需要使用分隔符,以便在将它们设置为图像源之前可以在到达时分割它们 可以将以下字符串作为base-64编

您能否将这两个图像合并到上的一个外部文件中,并作为图像链接到它们

差不多

<img src="base64.html#img1" />
<img src="base64.html#img2" />

数据URI(此处的键)通常是字符串,编码为base-64

因为它们是字符串,所以可以将它们连接为任何其他字符串。但是,不能将串联(数据uri)字符串直接设置为图像源

连接时,需要使用分隔符,以便在将它们设置为图像源之前可以在到达时分割它们

可以将以下字符串作为base-64编码的数据URI:

var img1 = 'data:...',
    img2 = 'data:...';
连接的版本可能如下所示,例如,
$
作为分隔字符:

var conc = img1 + '$' + img2;
现在可以通过包含两个数据URI组合的网络发送此字符串

要在从服务器到达时再次拆分它们,请执行以下操作:

var conc = getDataString();   /// some function to get the string from server
var images = conc.split('$'); /// split the string on the $ char
var img1 = images[0];         /// first index contains first string etc.
var img2 = images[1];

/// set the image sources
document.getElementById('image1').src = img1;
document.getElementById('image2').src = img2;
这当然需要进行错误检查和验证等,但对于原理而言,这是一种方法

请注意,不能仅通过将它们作为链接引用来自动拆分它们。当然,服务器可以解析链接并提供所需的单个数据uri——就像通常要做的那样。但,正如我理解您的问题的那个样,若您想从服务器获取表示两个(或更多)图像的单个字符串,那个么您需要在客户端手动拆分它们

注2:base-64编码图像比原始二进制图像+浏览器能够将其用作源所需的数据uri头大33%。问题可能是这样做是否有好处,或者由于base64开销,服务器检索二进制数据的额外请求的总成本是否会减少。只要我的2美分


希望这能有所帮助(而且我没有完全误解您的问题)。

这里的关键是数据URI,通常是字符串,而不是未编码为base-64的字符串

因为它们是字符串,所以可以将它们连接为任何其他字符串。但是,不能将串联(数据uri)字符串直接设置为图像源

连接时,需要使用分隔符,以便在将它们设置为图像源之前可以在到达时分割它们

可以将以下字符串作为base-64编码的数据URI:

var img1 = 'data:...',
    img2 = 'data:...';
连接的版本可能如下所示,例如,
$
作为分隔字符:

var conc = img1 + '$' + img2;
现在可以通过包含两个数据URI组合的网络发送此字符串

要在从服务器到达时再次拆分它们,请执行以下操作:

var conc = getDataString();   /// some function to get the string from server
var images = conc.split('$'); /// split the string on the $ char
var img1 = images[0];         /// first index contains first string etc.
var img2 = images[1];

/// set the image sources
document.getElementById('image1').src = img1;
document.getElementById('image2').src = img2;
这当然需要进行错误检查和验证等,但对于原理而言,这是一种方法

请注意,不能仅通过将它们作为链接引用来自动拆分它们。当然,服务器可以解析链接并提供所需的单个数据uri——就像通常要做的那样。但,正如我理解您的问题的那个样,若您想从服务器获取表示两个(或更多)图像的单个字符串,那个么您需要在客户端手动拆分它们

注2:base-64编码图像比原始二进制图像+浏览器能够将其用作源所需的数据uri头大33%。问题可能是这样做是否有好处,或者由于base64开销,服务器检索二进制数据的额外请求的总成本是否会减少。只要我的2美分


希望这有帮助(而且我没有完全误解您的问题)。

在数据URI规范中,纯HTML是不可能的。()

可以使用JavaScript只获取base64数据/文档的一个子字符串,并将其设置为img的src属性,正如链接答案所建议的那样

我只是不确定这样做是否值得,它看起来相当复杂,可能在加载时间上获得了很小的收益


如果您想使用它,一种方法可以是这样(简单的草稿,没有错误处理,没有奇特的函数): 在HTML文档中:

<script type="text/javascript" src="base64file.js" />
....
<img id="image0" src="" />
....
<img id="image1" src="" />
....
<script type="text/javascript">
   var singleImages = allImages.split('$');
   document.getElementById('image0').src = 'data:image/png;base64,' + singleImages[0];
   document.getElementById('image1').src = 'data:image/png;base64,' + singleImages[1];
   ....
</script>

....
....
....
var singleImages=allImages.split(“$”);
document.getElementById('image0')。src='数据:image/png;base64,“+singleImages[0];
document.getElementById('image1')。src='数据:image/png;base64,“+singleImages[1];
....
在base64file.js中:

allImages = 'abc... <- the base64 code for the first image
             $ <- split indicator that is not a part of the base64 character set
            cba.... <- the base64 code for the second image
            ';

allImages='abc 在数据URI规范内的纯HTML中是不可能的。()

可以使用JavaScript只获取base64数据/文档的一个子字符串,并将其设置为img的src属性,正如链接答案所建议的那样

我只是不确定这样做是否值得,它看起来相当复杂,可能在加载时间上获得了很小的收益


如果您想使用它,一种方法可以是这样(简单的草稿,没有错误处理,没有奇特的函数): 在HTML文档中:

<script type="text/javascript" src="base64file.js" />
....
<img id="image0" src="" />
....
<img id="image1" src="" />
....
<script type="text/javascript">
   var singleImages = allImages.split('$');
   document.getElementById('image0').src = 'data:image/png;base64,' + singleImages[0];
   document.getElementById('image1').src = 'data:image/png;base64,' + singleImages[1];
   ....
</script>

....
....
....
var singleImages=allImages.split(“$”);
document.getElementById('image0')。src='数据:image/png;base64,“+singleImages[0];
document.getElementById('image1')。src='数据:image/png;base64,“+singleImages[1];
....
在base64file.js中:

allImages = 'abc... <- the base64 code for the first image
             $ <- split indicator that is not a part of the base64 character set
            cba.... <- the base64 code for the second image
            ';

allImages='abc 将每个base64编码的图片放在自己的文件中,然后使用服务器端代码将base-64数据插入HTML页面中。服务器将看到三个单独的文件,但客户端(即web浏览器)将看到一个大文件。现在,您只有一个针对HTML页面的大型HTTP请求以及两张图片。如有必要,使用mod_rewrite使web浏览器相信它是一个静态可缓存HTML页面,而不是服务器端PHP/Perl/任何页面

简单的PHP示例(在Ubuntu 12.04上测试):

“/>
" />

注意:确保base64文件末尾没有换行符。

将每个base64编码的图片放在自己的文件中,然后使用服务器端代码在HTML页面中插入base-64数据。服务器将看到三个单独的文件,但客户端(即web浏览器
<img data-index="0" />
<img data-index="1" />
var ResourceLibrary = [];
ResourceLibrary["hello"] = "Hello World!";
ResourceLibrary["genderMale"] = true;
ResourceLibrary["username"] = "srvikram13";
ResourceLibrary["isDeveloper"] = true;
ResourceLibrary["firefox"] = "data:image/png;base64,iVBORw0KG...."
ResourceLibrary["chrome"] = "data:image/png;base64,iVBORw0KG...."
<img src='#' data-src='resourceId-firefox' alt='firefox' />
<img src='#' data-src='resourceId-chrome' alt='chrome' />
<p data-src='resourceId-hello'></p><br>
<input id='username' data-src='resourceId-username'/><br>
<input type="radio" data-src="resourceId-genderMale" />Male<br>
<input type="checkbox" data-src="resourceId-isDeveloper" />Developer<br>
$(document).ready(function(){
    $("*").each(function(i, e){
        if($(e).attr("data-src") && $(e).attr("data-src").indexOf("resourceId-") != -1) {
            switch($(e).get(0).tagName) { // Add more case statements to handle various types of elements
                case "INPUT" : 
                    if(!$(e).attr("type")){
                        $(e).val(ResourceLibrary[$(e).attr("data-src").replace("resourceId-", "")]);
                        return;
                    }
                    switch($(e).attr("type").toLowerCase()) {
                        case "radio":
                        case "checkbox":
                            $(e).attr("checked", ResourceLibrary[$(e).attr("data-src").replace("resourceId-", "")])
                    }
                    return;
                case "IMG" :
                    $(e).attr("src", ResourceLibrary[$(e).attr("data-src").replace("resourceId-", "")]);
                    return;
                case "P" :
                    $(e).html(ResourceLibrary[$(e).attr("data-src").replace("resourceId-", "")]);
                   return;
                default:
                   return;
            }
        }
    });
});
$('head').append("<style> ......... </style>");
@font-face {
    font-family: 'latoregular';
    src: url(data:application/x-font-woff;charset=utf-8;base64,........) format('woff');
    font-weight: normal;
    font-style: normal;
}
$('head').append("<style> @font-face { font-family:'latoregular'; src: url(data:application/x-font-woff;charset=utf-8;base64,........) format('woff'); font-weight:normal; font-style:normal; } .... </style>");
.logo1 { background-image: url(data:image/png;base64,.......); }
<img alt="" src="" class="icon1" />
$('.icon1').attr('src', 'data:image/png;base64,.......');
$('.icon2').attr('src', 'data:image/png;base64,.......');
#!/bin/bash
echo "data:${2:-$(file -bi $1)};base64,$(base64 -w0 $1)"
#!/bin/bash
filename=$1
basename=${filename##*/}
classname=${basename%.*}
echo "\$('.$classname').attr('src','$(./gendatauri.sh $filename)');"
#!/bin/bash
cat << EOF
@font-face {
    font-family: 'latoregular';
    src: url($(./gendatauri.sh latoregular.woff "application/x-font-woff;charset=utf-8")) format('woff');
    font-weight: normal;
    font-style: normal;
}
.logo1 {
    background-image: url($(./gendatauri.sh icon1.png));
}
EOF
#!/bin/bash

# CSS, including fonts and background images
(
    echo '$("head").append("<style>\n'
    ./gencss.sh
    echo '\n</style>");'
) | tr '\n' ' '
echo

# images
./genimgsrc.sh icon1.png
./genimgsrc.sh icon2.png

# JavaScript
cat YourOwnFunctions.js
./genjs.sh > GeneratedJavaScriptFile.js
<script src="GeneratedJavaScriptFile.js"></script>