I'm trying to tack on some information about the physical size for printing my PNGs just before they are generated.
Reading the libpng doc and the pHYs chunk specifications has been helpful but I just can't seem to crack it.
I have tried adding this chunk in the most manual and simplest way possible, however, the .png file ends up corrupted. Am I missing an encoding trick?
For the CRC computation I have used the 32-bit result of this site, having plugged in the ASCII values for the chunk that the code below gives me.
$encoded = $_POST['imgdata'];
$encoded = str_replace(' ', '+', $encoded);
$decoded= base64_decode($encoded);
$test = explode('IDAT',$decoded);
$ppu='00000000000000000010111000100011'; //32-bit integer for the pixels per unit
$dppu=bindec($ppu);
$test[0].=sprintf("%c",bindec('00000000000000000000000000001001')) //length, also 32-bit
.'pHYs' //type
. sprintf("%c",$dppu) //Pixels per unit, x axis
. sprintf("%c",$dppu) //Pixels per unit, y axis
.'1' //Units in metres (1 byte)
. sprintf("%c", bindec(base_convert('0x0BFAAA7E', 16, 2))) //CRC (32-bit)
.'IDAT';
$fintest=implode($test);
echo $fintest;
Please let me know whether hacking it in like this is likely to work. I am also unsure about my 32-bit integers: is zero-padding them as I am doing the correct way to make them 32-bit?
explode
andimplode
what I suspect they do: uncompressing and then recompressing only theIDAT
chunk? That's an error:pHYs
is a separate chunk, independent of the compressedIDAT
. – JongwarepHYs
chunk should be placed before theIDAT
, I am exploding it at the chunk name to append it at this point. I will look more into the"%c"
formatting standard, I was not aware the bit count wouldn't "translate" from an integer into the corresponding ASCII. Thanks – Alex