2012-03-15 114 views
0

我有一個圖像(jpeg)存儲在我的數據庫(SQL Server),當我讀它時,我得到了一個十六進制代碼。但我無法將其顯示給瀏覽器,它不理解並顯示純代碼。從數據庫到瀏覽器的十六進制顯示圖像

Here's a hex code example

我需要幫助實現這一目標。謝謝大家。

+0

你有十六進制轉儲...是它如何存儲在數據庫中?在中,數據庫中是否存在原始二進制對象,或者是二進制文件的十六進制文本? – Brad 2012-03-15 19:23:44

+0

它是如何存儲在數據庫中的? BLOB? – Michal 2012-03-15 19:24:22

回答

3

如果你有PHP> = 5.4,你可以使用hex2bin()

如果不是,您可以使用發佈該網頁上的替代功能: -

/** 
* Converts the hex representation of data to binary 
* 
* http://www.php.net/manual/en/function.hex2bin.php 
* 
* @param string $str  Hexadecimal representation of data 
* 
* @return string    Returns the binary representation of the given data 
*/ 
public function hex2bin($data) 
{ 
    $bin = ""; 
    $i  = 0; 
    do { 
     $bin .= chr(hexdec($data{$i}.$data{($i + 1)})); 
     $i  += 2; 
    } while ($i < strlen($data)); 
    return $bin; 
} 

然後你只需設置你的頭和呼應結果輸出到瀏覽器。

只要看看PHP manual,您會感到驚訝。希望這會對你有用,或者至少讓你走上正軌。