使用php获取png格式图像的元数据

时间:2013-06-04 00:49:11

标签: php png metadata exif

我可以使用exif标签获取jpeg或tiff格式图像的元数据。但是我该怎么做才能获得png元数据?

示例:获取exif标记的代码:

 <?php
    $image = "NATU.png";
    $exif = exif_read_data($image, 0, true);
    foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
    echo "$key.$name: $val <br>";
    } 
    } 
    ?>

1 个答案:

答案 0 :(得分:1)

PNG文件格式定义PNG文档被分成多个数据块。因此,您必须导航到您想要的大块。

假设你有一个格式正确的PNG:

<?php
  $fp = fopen('18201010338AM16390621000846.png', 'rb');
  $sig = fread($fp, 8);
  if ($sig != "\x89PNG\x0d\x0a\x1a\x0a")
  {
    print "Not a PNG image";
    fclose($fp);
    die();
  }

  while (!feof($fp))
  {
    $data = unpack('Nlength/a4type', fread($fp, 8));
    if ($data['type'] == 'IEND') break;

    if ($data['type'] == 'tEXt')
    {
       list($key, $val) = explode("\0", fread($fp, $data['length']));
       echo "<h1>$key</h1>";
       echo nl2br($val);

       fseek($fp, 4, SEEK_CUR);
    }
    else
    {
       fseek($fp, $data['length'] + 4, SEEK_CUR);
    }
  }


  fclose($fp);
?>