dop82210 2012-03-15 19:21
浏览 54
已采纳

从DB到浏览器以十六进制显示图像

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.

Here's a hex code example

I need help to achieve this. Thank you all in adv.

  • 写回答

1条回答 默认 最新

  • douxiangui5011 2012-03-15 19:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论