在php中附加png pHYs块 [英] Appending a png pHYs chunk in php

查看:128
本文介绍了在php中附加png pHYs块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在打印PNG之前添加一些有关物理尺寸的信息.

I'm trying to tack on some information about the physical size for printing my PNGs just before they are generated.

阅读 libpng文档和pHYs块规范很有帮助但我似乎无法破解它.

Reading the libpng doc and the pHYs chunk specifications has been helpful but I just can't seem to crack it.

我尝试过以最手动和最简单的方式添加此块,但是.png文件最终损坏了.我是否缺少编码技巧?

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?

对于CRC计算,我使用了此站点,并为下面的代码提供给我的代码块插入了ASCII值.

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;

请让我知道是否可以像这样破解它.我也不确定我的32位整数:在将它们设为32位的正确方法时,是否对它们进行零填充?

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?

推荐答案

花了一天的时间之后,我发现做到这一点的方法是逐字节地翻译所有内容.在文档之后,pHYs块需要:

After having spent the day on this, I found that the way to do this was to translate everything byte by byte. Following the docs, the pHYs chunk takes:

  • 4个字节用于块(仅数据)长度
  • 4个字节用于块类型(字符"pHYs")
  • 9个字节用于块数据:x和y密度每个4个字节,单位选择1个字节
  • 4个字节用于CRC

我将所有这些写为二进制字符串,并在必要时用0填充字节. 事实证明,chr()函数正确地将其编码为要在PNG文件中使用,这与我在问题中使用的sprintf("%c",$string)方法不同.这是代码

I wrote all this as a binary string, padding the bytes with 0 as necessary. It then turns out that the chr() function correctly encodes this to be used in the PNG file, unlike the sprintf("%c",$string) method I use in the question. Here is the code

$encoded = $_POST['imgdata'];
$encoded = str_replace(' ', '+', $encoded);
$decoded=  base64_decode($encoded);

$binstring='00000000000000000000000000001001' //4-byte length
        . '01110000010010000101100101110011' //4-byte type or 'pHYs'
        . '000000000000000000101110001000110000000000000000001011100010001100000001' //9-byte data
        . base_convert('0x0BFAAA7E', 16, 2); //4-byte CRC
foreach (str_split($binstring,8) as $b) {
    $on.=chr(bindec($b));
}

然后将$ on变量附加到IDAT块长度的4个字节之前的位置.

The $on variable is then appended to the position just before the 4 bytes of the IDAT chunk length.

这篇关于在php中附加png pHYs块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆