2013-03-27 87 views
1

因爲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

感謝,

+0

有多大的圖像?我想你可能會遇到這個方法的url長度問題。 – datasage 2013-03-27 19:08:02

+0

@datasage我的圖像大小是30.1KB,並且'ie'支持這個大小來解碼base64圖像字符串。 – 2013-03-27 20:28:02

+0

嗯!看起來我的圖片產生了41138個字節的url,遠遠高於接受的限制,但是雖然url不包含名稱對值,但它被傳遞到頭部,圖像的'data URI resource'不應該大於'在這種情況下爲4,096字節。 '參考:'http://support.microsoft.com/kb/2688188 – 2013-03-28 06:28:09

回答

2

在IE,至少在版本8和更早的版本中。網址的最大長度約爲2047字節。更多信息可以在這裏找到:http://support.microsoft.com/kb/208427

+0

我會檢查網址問題。謝謝。 – 2013-03-27 20:29:09