/
img-combiner.php
executable file
·334 lines (280 loc) · 8.53 KB
/
img-combiner.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env php
<?php
if (PHP_VERSION_ID < 50300) {
die("PHP version " . PHP_VERSION . " is not supported. Version 5.3+ is required.\n");
}
// Imagick is required
if (!extension_loaded('imagick')) {
die("PHP extension Imagick is required but not loaded.\n");
}
if (!is_writable(__DIR__)) {
die("Directory '" . __DIR__ . "' is not writable.\n");
}
// init
$dir = __DIR__;
$files = array();
$types = array('png');
$cols = 20; // images in line
$format = 'png'; // default output format
$margin = 0; // margin around each image
$export = false;
$expFN = 'combined_images.txt'; // filename with positions of each image
// options
$allowedOptions = array(
'-d' => array('<path>', 'Scan images in this directory.'),
'-f' => array('<files>', "List of images divided by comma in given directory, e.g. 'img1.png,img2.jpg,...'."),
'-t' => array('<types>', "Types (extensions) of images, e.g. 'png,jpg,...'."),
'-c' => array('<number>', "Count of images in one line. Default is $cols."),
'-o' => array('<format>', "Output image format, e.g. 'png'. Default is $format."),
'-m' => array('<number>', "Margin in pixels around each image. Default is {$margin}px."),
'-e' => array('<filename>', "Export positions of each image to the text file. Filename is optional. Default name is '$expFN'."),
'-h' => array('', 'Display this help message.')
);
$allowedTypes = array('png', 'gif', 'jpg', 'jpeg');
$allowedOutputFormats = array('png', 'gif', 'jpg');
$options = array_keys($allowedOptions);
if ($argc < 2 || $argv[1] == '-h') {
displayHelp($allowedOptions);
}
array_shift($argv); // remove script name
// process input params
while (list($key, $arg) = each($argv)) {
if (!in_array($arg, $options)) {
die("Invalid option '$arg'.\n");
}
$value = current($argv);
if (empty($value) && $arg != '-e') {
die("Missing value for option '$arg'.\n");
}
// check options
switch ($arg) {
case '-d': // directory
if (!is_dir($value)) {
die("'$value' is not directory.\n");
}
if (!is_readable($value)) {
die("Directory '$value' is not readable.\n");
}
$dir = rtrim($value, '/');
break;
case '-f': // files
$_files = explode(',', $value);
array_walk($_files, 'trim');
if (!empty($_files)) {
$files = $_files;
}
break;
case '-t': // types
$_types = explode(',', $value);
array_walk($_types, 'trim');
if (is_array($_types)) {
foreach ($_types as $type) {
if (!in_array($type, $allowedTypes)) {
die("Image type '$type' is not allowed.\n");
}
}
}
$types = $_types;
break;
case '-c': // count of images in line
if (!ctype_digit($value)) {
die("Option '$arg' must be a number.\n");
}
$value = (int) $value;
if ($value < 1) {
die("Invalid images count in line. Given '$value'.\n");
}
$cols = $value;
break;
case '-o': // output format
if (!in_array($value, $allowedOutputFormats)) {
$strFormats = implode(',', $allowedOutputFormats);
die("Only $strFormats output formats are supported. $value given.\n");
}
$format = $value;
break;
case '-m': // margin around each image
if (!ctype_digit($value)) {
die("Option '$arg' must be a number.\n");
}
$value = (int) $value;
if ($value < 0) {
die("Margin around images cannot be less than 0px. Given '$value'.");
}
$margin = $value;
break;
case '-e': // export images positions to text file
if (!empty($value)) {
$expFN = $value;
}
$export = true;
break;
default:
break;
}
next($argv);
}
// set files in given directory
if (empty($files)) {
$pattern = "~\.(" . implode('|', $types) . ")$~i";
foreach (glob("$dir/*") as $file) {
if (preg_match($pattern, $file)) {
$files[] = $file;
}
}
}
// load files set in input option
$images = array();
foreach ($files as $f) {
$f = pathinfo(trim($f, '/'), PATHINFO_BASENAME);
$file = "$dir/$f";
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (!is_readable($file)) {
die("File '$file' does not exist or is not readable.\n");
}
if (!in_array(strtolower($extension), $types)) {
$strTypes = implode(',', $types);
die("File '$file' with extension '$extension' is not allowed. Only $strTypes are supported.\n");
}
$images[] = $file;
}
if (empty($images)) {
die("There are no images to combine in directory '$dir'.\n");
}
try {
$totalImages = 0;
$startTime = microtime(true);
$positions = array();
$prevLineMaxHeight = $tmpMaxHeight = 0;
// start to create one image from multiple images
$imagesInLine = new Imagick();
$combinedImages = new Imagick();
$empty = true; // any image to process yet?
foreach ($images as $index => $image) {
$img = new Imagick($image);
$img->borderimage(($format == 'jpg' ? 'white' : 'transparent'), $margin, $margin);
if ($ok = $imagesInLine->addimage($img)) {
$totalImages++;
// add position
list($width, $height) = getimagesize($image);
$prevLineIndex = $index - $cols;
$prevIndex = $index - 1;
$left = $top = 0;
$tmpMaxHeight = max($tmpMaxHeight, $height);
if (isset($positions[$prevLineIndex])) { // is not first line
if ($index % $cols == 0) { // first image in line
$left = 0 + $margin;
} else {
$left = calculateLeftPosition($positions[$prevIndex], $margin);
}
$prevItem = $positions[$prevLineIndex];
$totalTop = $prevItem['top'] + $prevLineMaxHeight + $margin;
$top = $totalTop + 1;
} else { // first line
if ($index) {
$left = calculateLeftPosition($positions[$prevIndex], $margin);
} else { // first image
$left = 0 + $margin;
}
$top = 0 + $margin;
}
$positions[$index] = array(
'number' => $index + 1,
'left' => $left,
'top' => $top,
'width' => $width,
'height' => $height,
'imageName' => pathinfo($image, PATHINFO_BASENAME)
);
}
echo ($index + 1) . ") Adding: $image ... " . ($ok ? 'ok' : 'error') . "\n";
$empty = false;
if ($totalImages % $cols == 0) { // wrap images in line
$imagesInLine->resetiterator();
$combinedImages->addImage($imagesInLine->appendimages(false));
$imagesInLine = new Imagick();
$empty = true;
$prevLineMaxHeight = $tmpMaxHeight;
$tmpMaxHeight = 0;
}
}
// add rest of icons
if (!$empty) {
$imagesInLine->resetiterator();
$lastImagesInLine = $imagesInLine->appendimages(false);
$combinedImages->addimage($lastImagesInLine);
}
$outputFile = __DIR__ . "/combined_images.$format";
$combinedImages->resetiterator();
$finalImages = $combinedImages->appendimages(true);
$finalImages->setimageformat($format);
$finalImages->setimagefilename($outputFile);
if ($finalImages->writeimage()) {
$totalTime = round(microtime(true) - $startTime, 3);
echo "\n";
echo "$totalImages image" . ($totalImages === 1 ? '' : 's') . " combined into image '$outputFile' in $totalTime seconds.\n";
// export positions to text file
if ($export) {
$exportFilename = __DIR__ . "/$expFN";
if (!preg_match('~\.txt$~', $exportFilename)) {
$exportFilename = "$exportFilename.txt";
}
if (exportPositions($positions, $exportFilename)) {
echo "Positions exported to '$exportFilename'.\n";
}
}
exit(0);
} else {
die("Error while creating image '$outputFile'.");
}
} catch (Exception $e) {
$msg = $e->getMessage();
die("Imagick error: $msg.\n");
}
//---------------------------------- functions -------------------------------//
/**
* Help
* @param array $options
*/
function displayHelp(array $options)
{
echo "Usage: [options] [arguments]\n";
foreach ($options as $option => $info) {
echo " $option " . ($info[0] ?: "\t") . "\t$info[1]\n";
}
echo "\n";
exit(0);
}
/**
* From the previous image calculates a count of pixels from the left edge.
* @param array $prevItem
* @param int $margin
* @return int
*/
function calculateLeftPosition(array $prevItem, $margin = 0)
{
$left = $prevItem['left'] + $prevItem['width'] + $margin;
return $left + 1;
}
/**
* Exports positions of all images to text file.
* @param array $positions
* @param string $filename
* @return boolean
*/
function exportPositions(array $positions, $filename)
{
$l = '';
if (!empty($positions)) {
$c = array_keys($positions[0]); // cols
$header = array_combine($c, $c);
array_unshift($positions, $header);
foreach ($positions as $p) {
$l .= "$p[number]|$p[left]|$p[top]|$p[width]|$p[height]|$p[imageName]\n";
}
return file_put_contents($filename, $l) !== false;
}
return false;
}
exit(0);
?>