06

从这里看到的:

http://www.adayinthelifeof.nl/2010/01/14/handling-binary-data-in-php-with-pack-and-unpack/

http://www.garykessler.net/library/file_sigs.html

文中给了PNG示例。我仿了一下,给了JPG示例。

1
2
3
4
5
$fh = fopen('e:/temp/d3.jpg', 'rb');
$jpg =  fread ($fh, 14);
$header = unpack ("C4header/C2length/A4identifier/C1?/C2version/C1units/", $jpg);
fclose($fh);
print_r($header);

格式如下

Start of Image (SOI) marker — two bytes (FFD8)
JFIF marker (FFE0)

  • length — two bytes
  • identifier — five bytes: 4A, 46, 49, 46, 00 (the ASCII code equivalent of a zero terminated “JFIF” string)
  • version — two bytes: often 01, 02
    • the most significant byte is used for major revisions
    • the least significant byte for minor revisions
  • units — one byte: Units for the X and Y densities
    • 0 => no units, X and Y specify the pixel aspect ratio
    • 1 => X and Y are dots per inch
    • 2 => X and Y are dots per cm
  • Xdensity — two bytes
  • Ydensity — two bytes
  • Xthumbnail — one byte: 0 = no thumbnail
  • Ythumbnail — one byte: 0 = no thumbnail
  • (RGB)n — 3n bytes: packed (24-bit) RGB values for the thumbnail pixels, n = Xthumbnail * Ythumbnail

identifier无法5个一起取出,我就先取了4个,然后再取一个。

By 馒头


Leave a Reply