简体   繁体   中英

Creating an image of PNG-format in php with GD with exif

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)?

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM