0

I am looking for a way to create a PNG-image in PHP with some settings in EXIF tags. How do I achieve this and are able to read these tags from the PNG-file when created? I have tried the header("Key: ", value); but it doesn't work. Any suggestions? How can I read the data from the files? I guess they will end up in the "png:IHDR", but how can I read this? I can not use ImageMagick.

How can I see the tags (what tool is good)?

Popeye
  • 29
  • 8
  • 2
    exif is not used in png. ```It is not used in JPEG 2000, PNG, or GIF.``` https://en.wikipedia.org/wiki/Exif – R A Nov 27 '17 at 15:58
  • There is no room for metadata in the [`IHDR` chunk](https://en.wikipedia.org/wiki/Portable_Network_Graphics#Critical_chunks). EXIF metadata should stay in the [`eXIf` chunk](https://en.wikipedia.org/wiki/Portable_Network_Graphics#Ancillary_chunks). – axiac Nov 27 '17 at 16:25

2 Answers2

0

u can go through the following link exif is not used in png.

https://stackoverflow.com/questions/9542359/does-png-contain-exif-data-like-jpg

but now a days some png image preserve exif tag.u can try bellow link it might help http://php.net/manual/en/function.exif-read-data.php

Following is the sample code

<?php
//path of the image
$image_name = "1.PNG";

//read all the image attributes
$exif = exif_read_data($image_name, 0, true);
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers\n";


//print the name of the image
echo "Image Name:".$image_name."\n\n";

//iterate trough the image to list all the attributes
foreach ($exif as $key => $section) {
    echo "###############   Section Name :".$key." #############\n";
    foreach ($section as $name => $val) {
        echo "$key.$name: $val\n";
    }
    echo "\n";
}

?>

This will gives bellow output

Image contains headers
Image Name:1.PNG

###############   Section Name :FILE #############
FILE.FileName: 1.PNG
FILE.FileDateTime: 1511089868
FILE.FileSize: 6251146
FILE.FileType: 2
FILE.MimeType: image/jpeg
FILE.SectionsFound: ANY_TAG, IFD0, EXIF, MAKERNOTE

###############   Section Name :COMPUTED #############
COMPUTED.html: width="5184" height="3456"
COMPUTED.Height: 3456
COMPUTED.Width: 5184
COMPUTED.IsColor: 1
COMPUTED.ByteOrderMotorola: 0
COMPUTED.ApertureFNumber: f/5.0

###############   Section Name :IFD0 #############
IFD0.Make: Canon
IFD0.Model: Canon EOS 1300D
IFD0.Orientation: 1
IFD0.XResolution: 72/1
IFD0.YResolution: 72/1
IFD0.ResolutionUnit: 2
IFD0.DateTime: 2017:11:19 16:41:08
IFD0.Artist: 
IFD0.YCbCrPositioning: 2
IFD0.Copyright: 

###############   Section Name :EXIF #############
EXIF.ExposureTime: 1/60
EXIF.FNumber: 5/1
EXIF.ExposureProgram: 2
EXIF.ISOSpeedRatings: 800
EXIF.UndefinedTag:0x8830: 2
EXIF.UndefinedTag:0x8832: 800
EXIF.ExifVersion: 0230
EXIF.DateTimeOriginal: 2017:11:19 16:41:08
EXIF.DateTimeDigitized: 2017:11:19 16:41:08
EXIF.ComponentsConfiguration: 

I hope this might help

Mahesh Hegde
  • 1,131
  • 10
  • 12
  • If I want to create a chunk on my own and then read it, how do I do this? – Popeye Nov 28 '17 at 11:29
  • Warning: exif_read_data(png_.png): File not supported in /opt/lampp/htdocs/Indoor_Simulator/read_png.php on line 6 No header data found. Image Name: ./image/png/png_.png Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/Indoor_Simulator/read_png.php on line 14 – Popeye Nov 28 '17 at 14:10
  • I can't create any comments for my PNG and nothing will be displayed according to your code. When I try to save it like my code the chunk will end up in the IEND section and no values will come (the chunk comes though). – Popeye Nov 28 '17 at 14:11
  • try with image which has attributes , for eg Images from DSLR camera will have most of the image attributes – Mahesh Hegde Nov 28 '17 at 14:29
0

This is my function where I am trying to save my own chunk.

function save($image){
    header("Content-type: image/png");
    $name = "png";
    $save = "./image/png/". strtolower($name) ."_.png";
    imagepng($image, $save);

    // cmBn: c = not critical; m = private; B must be capital; n = depends on image data
    $chunk = "cmBn";
    $build_id = 2139; // Building ID (Uint32), positive values 32-bit up to 4,294,967,295 (232 − 1)
    $floor_id = 12345; // Floor ID (Uint8), positive values 8-bit up to 255
    $max_lat = 51.897867564536452;  // Max Latitude (Float)
    $min_long = 13.1234567890123; // Min Longitude (Float)
    $min_lat = 51.97867564518453;  // Min Latitude (Float)
    $max_long = 13.2223334445551; // Max Longitude (Float)

    // insert a BKGD chunk into the PNG file for graceful image degradation in IE6
    $pngData = bin2hex(file_get_contents($save));

    $idatMarker = '200049444154'; //?
    $bkgdMarker = '624b4744'; //cmBn

    $bkgdChunk = '0006' . $bkgdMarker; //?
    //oreach ($bgColor as $bit)
    //{
        //$bkgdChunkData .= '00' . dechex($bit);
        $bkgdChunkData .= '00' . dechex($build_id);
        $bkgdChunkData .= '00' . dechex($floor_id);
        $bkgdChunkData .= '00' . dechex($max_lat);
        $bkgdChunkData .= '00' . dechex($min_long);
        $bkgdChunkData .= '00' . dechex($min_lat);
        $bkgdChunkData .= '00' . dechex($max_long);
    //}
    $bkgdChunk .= $bkgdChunkData;
    $bkgdChunk .= dechex(crc32(pack('H*', $bkgdMarker . $bkgdChunkData))) . '0000';

    $parsed = explode($idatMarker, $pngData, 2); // split file by first 'IDAT' chunk
    $pngData = pack('H*', $parsed[0] . $bkgdChunk . $idatMarker . $parsed[1]);
    file_put_contents($save, $pngData);
}
Popeye
  • 29
  • 8