1

I can use exif tags for getting the metadata of a jpeg or a tiff format image.But what should I do to get png metadata?

Example: My code for getting exif tags:

 <?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>";
    } 
    } 
    ?>
Shubham Lalwani
  • 83
  • 4
  • 11
  • This library [PNGMetada](https://github.com/joserick/PNGMetadata) extract the metadata (Exif, XMP, GPS...) from a png, I hope this helps :) – joserick Oct 08 '19 at 10:35

1 Answers1

1

The PNG file format defines that a PNG document is split up into multiple chunks of data. You must therefore navigate your way to the chunk you desire.

Assuming you have a well format 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);
?>
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Notice that this would only gives the text in "tEXt" chunks, there are other two textual chunk types in PNG. – leonbloy Jun 04 '13 at 16:35