PHP adds iTXt comment to PNG image

I looked around everywhere. I know this can be done using some libraries in .net, but I really want my script to generate a β€œtagged” image. The reason is that we are the host of the forum and have teamed up with a theme company. The theme company wants us to be able to track any illegally distributed topics. I saw the iTXt comment in the GCHQ CanYouCrackIt exercise and I think it will be the best, without an intrusive method of protecting our property.

+1
source share
1 answer

Sentence. If you have a fixed piece of iTXt that you want to add to the image, a quick and dirty procedure can be to simply insert it just before the IEND block (12 bytes) of the original image. This should work because iTXt can be placed earlier than after image data. Of course, this does not check if the piece is already there.

Here's an example code that uses the tEXt piece (a bit simpler), it needs some polishing, but it basically works:

 <?php addTextToPngFile("x.png","x2.png","Watermark","Hi this is a TEXT test"); function addTextToPngFile($pngSrc,$pngTarget,$key,$text) { $chunk = phpTextChunk($key,$text); $png = file_get_contents($pngSrc); $png2 = addPngChunk($chunk,$png); file_put_contents($pngTarget,$png2); } // creates a tEXt chunk with given key and text (iso8859-1) // ToDo: check that key length is less than 79 and that neither includes null bytes function phpTextChunk($key,$text) { $chunktype = "tEXt"; $chunkdata = $key . "\0" . $text; $crc = pack("N", crc32($chunktype . $chunkdata)); $len = pack("N",strlen($chunkdata)); return $len . $chunktype . $chunkdata . $crc; } // inserts chunk before IEND chunk (last 12 bytes) function addPngChunk($chunk,$png) { $len = strlen($png); return substr($png,0,$len-12) . $chunk . substr($png,$len-12,12); } ?> 
+7
source

Source: https://habr.com/ru/post/907047/


All Articles