因为ie7
& ie8
无法默认为base64图像解码字符串,我一直在寻找一种替代的方式来做到这一点。 googleing有关这个问题后,我发现在http://dean.edwards.name/weblog/2005/06/base64-ie/一个很好的教程。PHP的base64解码功能是无法解码的base64图像串
据我了解解码的过程分为两个短脚本,如下列:
A. PHP的base64解码功能
命名为base64.php
由作者作为引导
<?php
$data = split(";", $_SERVER["QUERY_STRING"]);
$type = $data[0];
$data = split(",", $data[1]);
header("Content-type: ".$type);
echo base64_decode($data[1]);
?>
B. 的JavaScript
其中应的页面的头部标记中被插入其中的base64图像串应当被嵌入和其最后应的图像串传递给base64.php
用于处理&解码。
<script type="text/javascript">
// a regular expression to test for Base64 data
var BASE64_DATA = /^data:.*;base64/i;
// path to the PHP module that will decode the encoded data
var base64Path = "base64.php";
function fixBase64(img) {
// check the image source
if (BASE64_DATA.png(img.src)) {
// pass the data to the PHP routine
img.src = base64Path + "?" + img.src.slice(5);
}
};
// fix images on page load
onload = function() {
for (var i = 0; i < document.images.length; i++) {
fixBase64(document.images[i]);
}
};
</script>
不幸的是PHP的base64解码功能无法在我的情况下,图像串ie7
& ie8
解码。
我的文档类型是DTD XHTML 1.0 Transitional
& charset=utf-8
。
任何想法,这是怎么回事错在这里?
演示链接:
基于64位字符串不-PHP解码器:http://silentpond.eu5.org/base64-string-without-php-decoder.php
基于64位串用的PHP解码器:http://silentpond.eu5.org/base64-string-with-php-decoder.php
感谢,
有多大的图像?我想你可能会遇到这个方法的url长度问题。 – datasage 2013-03-27 19:08:02
@datasage我的图像大小是30.1KB,并且'ie'支持这个大小来解码base64图像字符串。 – 2013-03-27 20:28:02
嗯!看起来我的图片产生了41138个字节的url,远远高于接受的限制,但是虽然url不包含名称对值,但它被传递到头部,图像的'data URI resource'不应该大于'在这种情况下为4,096字节。 '参考:'http://support.microsoft.com/kb/2688188 – 2013-03-28 06:28:09