I have binary image data saved in my old database, it is saved by my old developer, now i want to display image from that using PHP, but I can not.

I have tried imagecreatefromstring but it returns FALSE.

Binary example data: http://freezinfo.com/gg.php

有帮助吗?

解决方案 2

Given the string displayed as text (extactly this sequence), it's a lineup of hexadecimal numbers.

In Jpeg, the magic numbers of such a file are FF D8 as the first two bytes and FF D9 as the last two bytes (compare with How to identify contents of a byte[] is a jpeg?).

These hexadecimal (hex in short) numbers can be found at the beginning and end of your sequence as well:

FF00D800FF00 ... 1F00FF00D9000
##  ##               ##  ##

Looking for these magic numbers also reveals that the hex values are append with 00 always. It also shows that at the very end an extra single 0 is found.

So four characters always form one value. Hex-encoded this looks like 16 bit values however the value never goes over the 8 bit range (0-255), therefore there is always 00 visible.

With this information alone one then can try to turn this into a binary string that PHP's imagecreatefromstring can deal with:

$string = implode('',         // map array of binary strings to binary string
    array_map('chr',          // map ord integer value to character
        unpack('v*',          // unsigned short (always 16 bit, little endian byte order)
            pack("H*", $data) // hex to binary (high nibble first)
)));

Using such a string then in

$result = imagecreatefromstring($string);
imagejpeg($result, 'test.jpg');

reveals the following PHP error notice:

imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: bad Huffman code

and the following image:

enter image description here

This looks desperately broken. So you are probably facing an additional encoding problem here. The last NUL byte suggests that more has been done and there also must be a reason why the data has been stored as 16 bit hex values instead of just binary data (blob) as databases have support for that.

But don't waste too much time, because of the flaw in the software and configuration that was used in the past, this might just be data-loss so all you can do is to restore from backups.

其他提示

The data you are trying to retrieve has HTML and its in HEX format but the image is corrupt or not valid.

To get the data:

// $url = "log.txt";
$url = "http://freezinfo.com/gg.php";

// Load Data from URL
$data = file_get_contents($url);
// Remove ALL HTML Tags
$data = strip_tags($data);

The Errors

Now Lets Examine the Header

 echo substr($data, 0, 4); // returns FF00

FF00 is not a valid jpeg prefix It should start with FFD8 OR FfD9

How did i know its a JPEG file and its not valid ?

 echo pack("H*", substr($data, 0, 60));

Output

����JFIF

It clearly has reference to JFIF which is JPEG File Interchange Format

How can it be fixed ?

A quick Image validation imagecreatefromstring

$data = pack("H*", $data); // Convert from hex
$im = @imagecreatefromstring($data);
var_dump($im); // returns false 

Looking at the image header again $data .. i could see a pattern

FF00D800FF00E000000010004A00460049
  ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^

I noticed that 00 is been inserted so remove that would actually give us a valid image header FFD8

You can fix this with a simple loop

// Trim all Spaces
$data = trim($data);

$newData = "";
for($i = 0; $i < strlen($data); $i += 4) {
    $newData .= $data{$i} . $data{$i + 1};
}

$newData = pack("H*", $newData);
$im = imagecreatefromstring($newData);
var_dump($im); // resource(6, gd)

Output

resource(6, gd)

Conclusion

You really need to examine the way you are converting your image to hex , it looks messed up form here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top