PHP将iTXt注释添加到PNG图像 [英] PHP add iTXt comment to a PNG image
问题描述
我到处都在寻找.我知道可以使用.net中的某些库来完成此操作,但是我确实希望我的脚本生成一个标记"图像.原因是我们是论坛主持人,并且已经与主题制作公司合作.主题制作公司希望我们能够追踪任何非法分发的主题.我在GCHQ CanYouCrackIt练习中看到了iTXt注释,并认为这将是最好的,没有侵入性的保护我们财产的方法.
I have looked around for this everywhere. I know it can be done using some libraries in .net, but I really want my script to generate a "marked" image. The reason is that we are a forum host, and have teamed up with a theme making company. The theme making company wants us to be able to track down any illegally distributed themes. I saw the iTXt comment in the GCHQ CanYouCrackIt exercise and think it'll be the best, no intrusive method of protecting our property.
推荐答案
建议:如果您有固定的 iTXt 您要添加到图像的块,一个快速而肮脏的过程可能是将其简单地插入到原始图像的IEND块(12字节)之前.这应该可行,因为可以将iTXt放置在图像数据之前或之后.当然,这不会检查该块是否已经存在.
A suggestion: If you have a fixed iTXt chunk that you want to add to an image, a quick and dirty procedure could be to simply insert it just before the IEND chunk (12 bytes) of the original image. This should work, because the iTXt can be placed before of after the image data. Of course, this does not check if the chunk as already there.
这是一个示例代码,使用tEXt
块(稍微简单一些),它需要进行一些改进,但基本上可以正常工作:
Here's an example code, using the tEXt
chunk (slightly 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);
}
?>
这篇关于PHP将iTXt注释添加到PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!