-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgps.php
executable file
·436 lines (398 loc) · 12.7 KB
/
imgps.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env php
<?php
/***************************************************************************
* Copyright (C) 2008 - 2010 by Ponetayev Ilya aka INSTE *
* instenet@gmail.com *
* ICQ : 473409594, Jabber/XMPP : inste@jabber.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
$isReadGIF = FALSE;
$isWriteGIF = FALSE;
$isReadWBMP = FALSE;
$isReadXPM = FALSE;
$isReadXBM = FALSE;
$OutInter = FALSE;
$slash = '/'; //POSIX slash (Linux, BSD) by default
$PREFIX = "imgps_";
$QDEF = 80; // Default JPEG quality
define("IMAGETYPE_GD", "101");
define("IMAGETYPE_GD2", "102");
/*
Return position of the final slash in the string
*/
function file_getlastslash($str)
{
global $slash;
if (($ps = strpos($str, $slash)) == FALSE)
return 0;
while (($pq = strpos($str, $slash, $ps + 1)) != FALSE)
$ps = $pq;
return $ps;
};
/*
Return filename with extension from fullname (with path)
*/
function file_pathtoname($str)
{
return substr($str, file_getlastslash($str) + 1, strlen($str) - file_getlastslash($str));
};
/*
Return short name (without extension) form filename (without path)
*/
function file_fulltoshort($str)
{
return substr($str,0,strlen($str) - 4);
};
/*
Return extension form file name
*/
function file_fulltoext($str)
{
return substr($str,strlen($str) - 3,strlen($str));
};
/*
Converting image (core function)
*/
function imageconvert($infilename, $outfilename, $outformat, $dh, $dw, $quality)
{
global $isWriteGIF;
global $isReadGIF;
global $OutInter;
$infmt = exif_imagetype($infilename);
switch ($infmt) {
case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($infilename);
$IF = "JPEG";
break;
case IMAGETYPE_PNG: $image = imagecreatefrompng($infilename);
$IF = "PNG";
break;
default: break;
}
if (($infmt == IMAGETYPE_GIF) && $isReadGIF) {
$image = imagecreatefromgif($infilename);
$IF = "GIF";
};
if ($image != FALSE) {
$in = getimagesize($infilename);
$inheight = $in[1];
$inwidth = $in[2];
$sw = imagesx($image);
$sh = imagesy($image);
if (($dh == -5) && ($dw == -5)) {
/*
Don't resize
*/
$dh = $sh;
$dw = $sw;
};
if ($dh == -1) // Autocomputing size
$dh = (int)$dw*($sh/$sw);
$oimage = imagecreatetruecolor($dw, $dh);
if ($outformat == -1) // Autocompute format
$outformat = $infmt;
if ($outformat == IMAGETYPE_GIF) {
$OF = "GIF";
if (!$isWriteGIF)
$outformat = IMAGETYPE_JPEG;
};
switch ($outformat) {
case IMAGETYPE_JPEG: $OF = "JPEG"; break;
case IMAGETYPE_PNG: $OF = "PNG"; break;
case IMAGETYPE_GD: $OF = "GD"; break;
case IMAGETYPE_GD2: $OF = "GD2"; break;
default: break;
}
if (($OutInter) AND ($outformat != IMAGETYPE_GD2) AND ($outformat != IMAGETYPE_GD))
$OF = $OF."(i)";
print("-> Converting '".file_pathtoname($infilename)."' (".$IF." -> ".$OF."; ".$sw."x".$sh." -> ".$dw."x".$dh.")...");
imagecopyresampled($oimage, $image, 0, 0, 0, 0, $dw, $dh, $sw, $sh);
if (($OutInter) AND ($outformat != IMAGETYPE_GD2) AND ($outformat != IMAGETYPE_GD))
imageinterlace($oimage);
switch ($outformat) {
case IMAGETYPE_JPEG: imagejpeg($oimage, $outfilename, $quality); break;
case IMAGETYPE_PNG: imagepng($oimage, $outfilename); break;
case IMAGETYPE_GIF: imagegif($oimage, $outfilename); break;
case IMAGETYPE_GD: imagegd($oimage, $outfilename); break;
case IMAGETYPE_GD2: imagegd2($oimage, $outfilename); break;
default: break;
}
print(" DONE!".chr(10));
return 1;
} else
{
print("-> Cannot read file '".file_pathtoname($infilename)."': Format doesn't supported!".chr(10));
return 0;
};
};
/*
Preparing filenames for new files
*/
function file_preparefilename($out, $format, $name)
{
global $PREFIX;
$newname = "-1";
if ($format == $out) {
$old = file_fulltoshort($name);
if ($name != $old.$newext)
$newname = $old.$newext;
else
$newname = $PREFIX.$old.$newext;
};
return $newname;
};
/*
Preparing and convert image
*/
function file_convert($readname, $OUTFMT, $h, $w, $q, $rmsrc)
{
global $PREFIX;
if (file_exists($readname)) {
$name = $readname;
if ($OUTFMT != -1) {
switch ($OUTFMT) {
case IMAGETYPE_JPEG: $newext = ".jpg"; break;
case IMAGETYPE_PNG: $newext = ".png"; break;
case IMAGETYPE_GIF: $newext = ".gif"; break;
case IMAGETYPE_GD: $newext = ".gd"; break;
case IMAGETYPE_GD2: $newext = ".gd2"; break;
default: break;
}
$old = file_fulltoshort($name);
if ($name != $old.$newext)
$newname = $old.$newext;
else
$newname = $PREFIX.$old.$newext;
} else
$newname = $PREFIX.$name;
$rslt = imageconvert($name, $newname, $OUTFMT, $h, $w, $q);
if ($rmsrc)
unlink($name);
return $rslt;
}
else
{
print("-> File doesn't exist, skipping!".chr(10));
return 0;
};
};
/*
Main code
*/
list($usec, $sec) = explode(" ",microtime()); $sysstart = ((float)$usec + (float)$sec);
print("Starting Image Processing Tool @ PHP v 0.3/120810".chr(10));
print("Published under GNU GPL v2.".chr(10));
print("Your PHP version: '".PHP_VERSION."'.".chr(10));
print("(c) Ponetayev Ilya aka INSTE, 2008 - 2010.".chr(10));
print("-> Checking OS version... ");
if ((PHP_OS == "Linux") OR (PHP_OS == "FreeBSD")) {
$slash = '/';
print(PHP_OS.", POSIX slash: '".$slash."'.".chr(10));
};
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$slash = '\\';
print(PHP_OS.", Win slash: '".$slash."'.".chr(10));
};
$gi = gd_info();
if (($gi != FALSE) AND (strpos($gi["GD Version"], "2.0") != FALSE))
print("-> Checking GD version... '".$gi["GD Version"]."', passed.".chr(10));
else
die("-> Your PHP configuration doesn't support GD or version too old!".chr(10));
print("-> Checking for reading GIF support in GD2...");
if ($gi["GIF Read Support"]) {
print(" Yes, reading GIF/GD2 works.".chr(10));
$isReadGIF = TRUE;
} else {
print(" No, not found.".chr(10));
$isReadGIF = FALSE;
};
print("-> Checking for writing GIF support in GD2...");
if ($gi["GIF Create Support"]) {
print(" Yes, creating GIF/GD2 works.".chr(10));
$isWriteGIF = TRUE;
} else {
print(" No, not found.".chr(10));
$isWriteGIF = FALSE;
};
print("-> Checking for reading WBMP support in GD2...");
if ($gi["WBMP Support"]) {
print(" Yes, reading WBMP/GD2 works.".chr(10));
$isReadWBMP = TRUE;
} else {
print(" No, not found.".chr(10));
$isReadWBMP = FALSE;
};
$types = imagetypes();
print("-> Checking for support JPEG in GD2...");
if ($types & IMG_JPEG)
print(" Yes, JPEG/GD2 works.".chr(10));
else
die(chr(10)."-> You don't have support JPEG in GD2 and PHP!".chr(10));
print("-> Checking for support PNG in GD2...");
if ($types & IMG_PNG)
print(" Yes, PNG/GD2 works.".chr(10));
else
die(chr(10)."-> You don't have support PNG in GD2 and PHP!".chr(10));
print("-> Analyzing command-line arguments...");
if ($argc < 2)
die(chr(10).'-> Run "php imgps.php -h" for help in use.'.chr(10));
/*
Describes output format, if "-j" then JPEG, if "-p" then PNG,
if "-n" then same as input file (No change, resize only).
*/
$firstcmd = $argv[1];
/*
Describes processing options, if "-i" then read filenames from ARGV/ARGC,
if "-f" then from file from ARGV[4].
If this argument contains "r" then source files will be removed after conversion.
If this argument contains "l" then output files will be interleaved.
For example: "-irl"
*/
if ($argc > 2)
$scndcmd = $argv[2];
/*
Describes output size, may be "1000x500" or only _WIDTH_ ("1000"). If you
specify only width, height will be computed to save proportional
of source image, height output := (int)(wigth output * (height input / width input))
For JPEG you can choose quality in band 0..100, command should be like that:
"1024x768:75" or "1024:75".
*/
if ($argc > 2)
$thrdcmd = $argv[3];
if (($firstcmd == "-h") OR ($firstcmd == "--help")) {
print(" DONE!".chr(10));
echo
'Usage:
(files from argv): php imgps.php [frmt] -i{rl} [width{xheight}{:quality}] ...files...
(files from list): php imgps.php [frmt] -f{rl} [width{xheight}{:quality}] filelist.txt
[frmt]:
-> "-j" : Convert all to JPEG.
-> "-p" : Convert all to PNG.
-> "-g" : Convert all to GIF (if supported).
-> "-n" : Don\'t convert, resize only.
[{rl}]"
-> "r" : Remove source files after conversion.
-> "l" : Make target files interleaved.
[width{xheight}{:quality}]:
-> width : width of output image in pixels, "width".
-> height (optional) : heigth of output image in pixels.
-> quality (optional) : quality of output image in range "0..100". (default:\''.$QDEF.'\').
has effect only for JPEG output.
-> examples: "1024x768:60", "1200:45", "1600", "1600x1050".
-> If you don\'t want resize (convert only), specify "-n" in this parameter.
';
die('');
};
if ($firstcmd == "-j")
$OUTFMT = IMAGETYPE_JPEG;
if ($firstcmd == "-p")
$OUTFMT = IMAGETYPE_PNG;
if ($firstcmd == "-g")
if (!$isWriteGIF)
die("-> You can't create GIF files, your PHP/GD2 doesn't support it!".chr(10));
else
$OUTFMT = IMAGETYPE_GIF;
if ($firstcmd == "-gd")
$OUTFMT = IMAGETYPE_GD;
if ($firstcmd == "-gd2")
$OUTFMT = IMAGETYPE_GD2;
if (($firstcmd != "-j") && ($firstcmd != "-p")
&& ($firstcmd != "-g") && ($firstcmd != "-gd")
&& ($firstcmd != "-gd2"))
$OUTFMT = -1; // Autocomputing
if ($thrdcmd != "-n") {
if (strpos($thrdcmd, "x") != FALSE) {
if (strpos($thrdcmd, ":") != FALSE) {
$w = (int)substr($thrdcmd, 0, strpos($thrdcmd, "x"));
$h = (int)substr($thrdcmd, strpos($thrdcmd, "x") + 1, strpos($thrdcmd, ":"));
$q = (int)substr($thrdcmd, strpos($thrdcmd, ":") + 1, strlen($thrdcmd));
} else {
$w = (int)substr($thrdcmd, 0, strpos($thrdcmd, "x"));
$h = (int)substr($thrdcmd, strpos($thrdcmd, "x") + 1, strlen($thrdcmd));
$q = $QDEF;
};
} else {
if (strpos($thrdcmd, ":") != FALSE) {
$w = (int)substr($thrdcmd, 0, strpos($thrdcmd, ":"));
$q = (int)substr($thrdcmd, strpos($thrdcmd, ":") + 1, strlen($thrdcmd));
$h = -1; // Autocomputing
} else {
$w = (int)$thrdcmd;
$h = -1; // Autocomputing
$q = $QDEF;
};
};
if (($q < 0) OR ($q > 100))
$q = $QDEF;
} else {
/*
Don't resize, convert only.
*/
$h = -5;
$w = -5;
$q = $QDEF;
};
/*
Remove source after image converting
*/
if (strpos($scndcmd, "r") != FALSE)
$RemoveSource = TRUE;
else
$RemoveSource = FALSE;
/*
Interlace output image
*/
if (strpos($scndcmd, "l") != FALSE)
$OutInter = TRUE;
else
$OutInter = FALSE;
if (strpos($scndcmd, "f") != FALSE)
$scndcmd = "-f";
if (strpos($scndcmd, "i") != FALSE)
$scndcmd = "-i";
print(" DONE!".chr(10));
if ($scndcmd == "-i") {
print("-> Converting files from cmd-line...".chr(10));
for($i = 4;$i < $argc;$i++) {
if (file_exists($argv[$i])) {
$rslt = file_convert($argv[$i], $OUTFMT, $h, $w, $q, $RemoveSource);
if ($rslt == 1) {
$prcnt = (int)((($i - 3) / ($argc - 4)) * 100);
list($usec, $sec) = explode(" ",microtime());
$sysstop = ((float)$usec + (float)$sec);
$dt = round($sysstop - $sysstart,1);
$eta = $dt * ($argc - $i);
$neta = $dt * ($i - 4);
print("----> Total $prcnt% done, Time: $neta s, ETA: $eta s.".chr(10));
list($usec, $sec) = explode(" ",microtime()); $sysstart = ((float)$usec + (float)$sec);
};
};
};
};
if ($scndcmd == "-f") {
if (file_exists($argv[4])) {
print("-> Converting files from the filelist '".$argv[4]."'...".chr(10));
$fd = fopen($argv[4], "r");
while (!feof($fd)) {
$readname = fgets($fd, 1024);
$readname = substr($readname, 0, strlen($readname) - 1);
file_convert($readname, $OUTFMT, $h, $w, $q, $RemoveSource);
};
fclose($fd);
};
};
print("-> Has completed!".chr(10).chr(10));
?>