以十六进制从DB到浏览器显示图像 [英] Display image in hex from DB to browser
问题描述
我有一个存储在我的DB (SQL Server)中的图像(jpeg),当我读取它时,我得到了一个十六进制代码。但我无法将其显示给浏览器,它不理解并显示纯代码。
我需要帮助来实现这一点。如果你有PHP> = 5.4,你可以使用 www.php.net/manual/en/function.hex2bin.phprel =nofollow> hex2bin()。
如果不是,您可以使用该页面上发布的替代函数: -
/ **
*将数据的十六进制表示转换为二进制
*
* http://www.php.net/manual/en/function.hex2bin.php
*
* @param string $ str数据的十六进制表示
*
* @return string返回给定数据的二进制表示形式
* /
public function hex2bin($ data)
{
$ bin =;
$ i = 0;
do {
$ bin。= chr(hexdec($ data {$ i}。$ data {($ i + 1)}));
$ i + = 2;
} while($ i 返回$ bin;
}
然后您只需设置标题并将结果回显给浏览器。 / p>
只要看 PHP手册。希望这会对你有用,或者至少让你走上正轨。
I have an image (jpeg) stored in my DB (SQL Server) and when I read it I got a hex code. But I can't display it to the browser, it doesn't understand and shows the pure code.
I need help to achieve this. Thank you all in adv.
If you have PHP >= 5.4 you can use hex2bin().
If not you can use the alternative function posted on that page:-
/**
* 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;
}
Then you just set your headers and echo the result out to the browser.
You'd be surprised what you can learn just by looking at the PHP manual. Hopefully, this will work for you, or at least set you on the right track.
这篇关于以十六进制从DB到浏览器显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!