-
Notifications
You must be signed in to change notification settings - Fork 73
/
escimages.php
135 lines (119 loc) · 4.13 KB
/
escimages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Utility to extract images from binary ESC/POS data.
*/
require_once __DIR__ . '/vendor/autoload.php';
use ReceiptPrintHq\EscposTools\Parser\Parser;
use ReceiptPrintHq\EscposTools\Parser\Command\ImageContainer;
function outpImg($outputDir, $imgNo, ImageContainer $img, $outputPbm, $outputPng, $receiptName)
{
// Output an image
$desc = $img -> getWidth() . 'x' . $img -> getHeight();
echo "[ Image $imgNo: $desc ]\n";
$outpFilename = $outputDir . '/' . "$receiptName-" . sprintf('%02d', $imgNo);
if ($outputPbm) {
file_put_contents($outpFilename . ".pbm", $img -> asPbm());
}
if ($outputPng) {
file_put_contents($outpFilename . ".png", $img -> asPng());
}
}
// Read CLI options in
$shortopts = "f:o:h";
$longopts = array(
"file:",
"output-dir:",
"png",
"pbm",
"help"
);
$options = getopt($shortopts, $longopts);
$usage = "Usage: " . $argv[0] . " OPTIONS --file 'filename'\n";
// Input file
$inputFile = null;
$inputFile = array_key_exists("f", $options) ? $options["f"] : $inputFile;
$inputFile = array_key_exists("file", $options) ? $options["file"] : $inputFile;
// Output dir
$outputDir = ".";
$outputDir = array_key_exists("o", $options) ? $options["o"] : $outputDir;
$outputDir = array_key_exists("output-dir", $options) ? $options["output-dir"] : $outputDir;
// Help
$showHelp = array_key_exists("h", $options) || array_key_exists("help", $options);
// Output formats
$outputPng = array_key_exists("png", $options);
$outputPbm = array_key_exists("pbm", $options);
if (!$outputPng && !$outputPbm) {
// Default
$outputPng = true;
}
if (empty($options) || ( $inputFile === null && !$showHelp)) {
// Incorrect usage shows error and quits nonzero
$error = "$usage\nTry '" . $argv[0] . " --help' for more information.\n";
file_put_contents("php://stderr", $error);
exit(1);
}
if (array_key_exists("h", $options) || array_key_exists("help", $options)) {
// Request for help
$message = "$usage
Required options:
-f, --file FILE The input file to read.
Output options:
-o, --output-dir DIRECTORY The directory to write output files to.
Output format:
Select one or more formats for output. If none is specified, then PNG is used.
--png Write output files in PNG format.
--pbm Write output files in PBM format.
Other options:
-h, --help Show this help\n";
echo $message;
exit(0);
}
// Quick validation
if (!file_exists($outputDir) || !is_dir($outputDir)) {
$error = "Output location does not exist, or is not a directory.\n";
file_put_contents("php://stderr", $error);
exit(1);
}
$outputDir = rtrim($outputDir, '/');
if (!file_exists($inputFile) || !is_readable($inputFile)) {
$error = "Input file does not exist, or is not readable.\n";
file_put_contents("php://stderr", $error);
exit(1);
}
$receiptName = $path_parts = pathinfo($inputFile)['filename'];
// Load in a file
$fp = @fopen($inputFile, 'rb');
if (!$fp) {
$error = "Failed to open the input file\n";
file_put_contents("php://stderr", $error);
exit(1);
}
$parser = new Parser();
$parser -> addFile($fp);
// Extract images
$bufferedImg = null;
$imgNo = 0;
$commands = $parser -> getCommands();
foreach ($commands as $cmd) {
if ($cmd -> isAvailableAs('GraphicsDataCmd') || $cmd -> isAvailableAs('GraphicsLargeDataCmd')) {
$sub = $cmd -> subCommand();
if ($sub -> isAvailableAs('StoreRasterFmtDataToPrintBufferGraphicsSubCmd')) {
// Assign image when stored
$bufferedImg = $sub;
} else if ($sub -> isAvailableAs('PrintBufferredDataGraphicsSubCmd')) {
// Print assigned image
$imgNo = $imgNo + 1;
outpImg($outputDir, $imgNo, $bufferedImg, $outputPbm, $outputPng, $receiptName);
$bufferedImg = null;
}
} else if ($cmd -> isAvailableAs('ImageContainer')) {
$imgNo = $imgNo + 1;
outpImg($outputDir, $imgNo, $cmd, $outputPbm, $outputPng, $receiptName);
}
}
// Just for debugging
function shortName($longName)
{
$nameParts = explode("\\", $longName);
return array_pop($nameParts);
}