douhan0562 2017-08-16 10:47
浏览 318

如何将db中存储的图像转换为十六进制为可见图像

Context I have access to database. That database contains image data something like 0xFFD8FFE....................09090

Problem

I need those cryptic(hex) data to convert into visual image. I am not being able to find any way.

I have tried

$image = base64_decode(file_get_contents('img.txt'));
$db_img = imagecreatefromstring($image);
Header("Content-type: image/jpeg");
imagepng($db_img);

Here img.txt is file name containing the hex data.

It Doesn't work. How can I achieve this?

  • 写回答

1条回答 默认 最新

  • doudouba4520 2017-08-16 11:27
    关注

    To turn a string like 0xFFD8FFE...09090 into actual binary data, you need to first get rid of the "0x" prefix, which is not part of the actual data, and then do a simply hex → bin conversion:

    $data   = '0xFFD8FFE...09090';  // maybe from file_get_contents
    $binary = hex2bin(substr($data, 2));
    

    If you don't know what type of image it is then, the most efficient method to find out is probably the use of finfo:

    $finfo = new finfo(FILEINFO_MIME);
    $type  = $finfo->buffer($binary);
    
    header('Content-Type: ' . $type);
    echo $binary;
    

    There's no need to use gd to reencode the image if $binary already is an image. You may want to inspect $type here a bit to verify that it's really the kind of file you expect before blindly outputting it.

    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应