PHP-将单字节转换为整数(PCX图形格式)


PHP - Convert single byte to Integer (PCX Graphic Format)

我正在进行一个项目,该项目涉及与Zebra打印机直接对话。目前,我正在尝试使用一种名为"ztools"的东西将图像转换为GRF格式。ZTools似乎很古老,并不总是正确转换图形。

我已经开始挖掘它转换的PCX文件格式的信息,现在我发现自己在字节土地上。这是我目前的参考:PCX技术参考

所以,我有一个从photoshop保存的基本PCX文件,它是2x2,看起来像这样:

10
01

然而,这正是我陷入困境的地方。我从未使用过字节,我正尝试使用fopen("file","rb")用PHP读取pcx文件;和fread。然而,似乎无论我做什么,我都会得到一堆零。有人知道我需要做什么才能将字节转换为数字等价物吗?

这是我的弱点:

<?php
$file = "test.pcx";
// Open the file for binary reading (b flag)
$handle = fopen($file, "rb");
while (!feof($handle)) {
    $contents = fread($handle, 1);
    $contents = $contents >> 8;
    echo $contents >> 8;
    $content .= $contents;
}

fclose($handle);

我能够准确地获得标题信息:

<?php
$file = "test.pcx";
function intToBits($int) {
    $return = "00000000";
    if ($int <= 255 && $int >= 0) {
        $check = 128;
        $x = 0;
        while ($int > 0) {
            if ($int > $check) {
                $return[$x] = "1";
            }
            $int -= $check;
            $check /= 2;
            $x++;
        }
    }
    return $return;
}
// Open the file for binary reading (b flag)
$handle = fopen($file, "rb");
$PCX_MAP = array();
$PCX_MAP['Manufacturer'][0] = 1; // Manufacturer
$PCX_MAP['Version'] = 1; // Version Info
$PCX_MAP['Encoding'] = 1; // Encoding
$PCX_MAP['BitsPerPixel'] = 1; // BitsPerPixel
$PCX_MAP['Xmin'] = 2; // Window Xmin
$PCX_MAP['Ymin'] = 2; // Window Ymin
$PCX_MAP['Xmax'] = 2; // Window Xmax
$PCX_MAP['Ymax'] = 2; // Window Ymax
$PCX_MAP['HDpi'] = 2; // HDpi (Resolution)
$PCX_MAP['VDpi'] = 2; // VDpi (Resolution)
$PCX_MAP['colormap'] = 48; // Colormap
$PCX_MAP['Reserved'] = 1; // Reserved = 0
$PCX_MAP['NumberColorPlanes'] = 1; // Number of color planes
$PCX_MAP['BytesPerLine'] = 2; // Bytes Per Line
$PCX_MAP['PalleteInfo'] = 2; // Palette Info
$PCX_MAP['HorizontalScreenSize'] = 2; // H Screen Size
$PCX_MAP['VerticalScreenSize'] = 2; // V Screen Size
$PCX_MAP['Filler'] = 54; // Filler
$length = reset($PCX_MAP);
while (!feof($handle)) {
    if ($length !== false) {
        $contents = fread($handle, $length);
        $contents = ord($contents);
        echo key($PCX_MAP) . " : {$contents}'n";
        $content .= $contents;
        $length = next($PCX_MAP);
    }
    else {
        // Get the rest 1 By 1
        $contents = fread($handle, 1);
        $contents = ord($contents);
        $contents = intToBits($contents);
        echo $contents ."'n";
    }
}

fclose($handle);
/*
$data = file_get_contents($file);
for($i = 0; $i < strlen($data); ++$i) {
    $char = ord($data[$i]);
    echo "Byte $i: $char'n";
}*/

然而,我仍在尝试解析图像数据。

目前返回的是:

Manufacturer : 10
Version : 5
Encoding : 1
BitsPerPixel : 1
Xmin : 0
Ymin : 0
Xmax : 3
Ymax : 1
HDpi : 200
VDpi : 200
colormap : 15
Reserved : 0
NumberColorPlanes : 1
BytesPerLine : 2
PalleteInfo : 1
HorizontalScreenSize : 0
VerticalScreenSize : 0
Filler : 0
10000000
11000000
11111110
00000000
11000000
11111110
00000000

标头信息是正确的,但我不确定填充后的数据。

在这个例子中运行的图形是一个4 x 2-0表示白色,1表示黑色

0101
1010

试试这个:

while (!feof($handle)) {
    $contents = fread($handle, 1);
    $contents = $contents && 0xFF;
    echo $contents;
    $content .= $contents;
}