-
Notifications
You must be signed in to change notification settings - Fork 8
/
converter.php
589 lines (449 loc) · 14.9 KB
/
converter.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
/*
* Server api for converting files
* https://github.com/commeta/modxWebpConverter
* https://webdevops.ru/blog/webp-converter-plugin-modx.html
*
* Copyright 2022 commeta <[email protected]>
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
////////////////////////////////////////////////////////////////////////
// Init
$param_jpeg= "-metadata none -quiet -pass 10 -m 6 -mt -q 85 -low_memory";
$param_png= "-metadata none -quiet -pass 10 -m 6 -alpha_q 85 -mt -alpha_filter best -alpha_method 1 -q 70 -low_memory";
$suppliedBinaries= [
'winnt' => 'cwebp-110-windows-x64.exe', // Microsoft Windows 64bit
'darwin' => 'cwebp-110-mac-10_15', // MacOSX
'sunos' => 'cwebp-060-solaris', // Solaris
'freebsd' => 'cwebp-060-fbsd', // FreeBSD
'linux' => [
// Dynamically linked executable.
// It seems it is slightly faster than the statically linked
'cwebp-110-linux-x86-64',
// Statically linked executable
// It may be that it on some systems works, where the dynamically linked does not (see #196)
'cwebp-103-linux-x86-64-static',
// Old executable for systems in case both of the above fails
'cwebp-061-linux-x86-64'
]
];
$excludeSubdirectories= [ // Exclude subdirectories from search
DIRECTORY_SEPARATOR.'core'.DIRECTORY_SEPARATOR.'packages',
DIRECTORY_SEPARATOR.'tmp',
DIRECTORY_SEPARATOR.'manager',
DIRECTORY_SEPARATOR.'webp'
];
$BASE_PATH= dirname(dirname(__DIR__));
define('BASE_PATH', $BASE_PATH);
// Decrement max execution time, keep correct server or proxy connection timeout - equal php execution time!
$max_time= ini_get('max_execution_time');
$max_time--;
set_time_limit($max_time);
ini_set('MAX_EXECUTION_TIME', $max_time);
header('Content-type: application/json');
$time_limit_exception= new time_limit_exception;
if(!isset($_POST['json'])) _die(json_encode([]));
$json= json_decode($_POST['json'], true);
require_once(BASE_PATH.DIRECTORY_SEPARATOR.'config.core.php');
require_once(MODX_CORE_PATH.'model'.DIRECTORY_SEPARATOR.'modx'.DIRECTORY_SEPARATOR.'modx.class.php');
$modx= new modX();
if((!$modx) || (!$modx instanceof modX)) {
_die(json_encode(['status'=> 'Could not create MODX class']));
}
$modx->initialize('mgr');
//$modx->getService('error', 'error.modError', '', '');
if(!$modx->user->hasSessionContext('mgr')) { // Check authorization
_die(json_encode(['status'=> 'Unauthorized']));
}
////////////////////////////////////////////////////////////////////////
// Json API
if($json['mode'] == 'clean'){ // Clean deleted copy of files into /webp/ directory
if( !is_dir(BASE_PATH.DIRECTORY_SEPARATOR.'webp') ) goto die_clean;
ignore_user_abort(true);
set_time_limit(300);
ini_set('MAX_EXECUTION_TIME', 300);
recursive_search_webp(BASE_PATH.DIRECTORY_SEPARATOR.'webp'); // Remove deleted copy webp files recursive
$options = array(xPDO::OPT_CACHE_KEY=>'webp_on_page'); // Clear webp modx cache
$modx->cacheManager->clean($options);
recursive_remove_empty_dirs(BASE_PATH.DIRECTORY_SEPARATOR.'webp'); // Remove empty dirs
die_clean:
_die(json_encode([
'status'=> 'complete',
'mode'=> 'clean'
]));
}
if($json['mode'] == 'flg'){ // Autostart search and convert new files, when cleaning cache
if(is_file(MODX_CONNECTORS_PATH.'converter/converter.flg')){
unlink(MODX_CONNECTORS_PATH.'converter/converter.flg');
_die(json_encode([
'status'=> 'complete',
'mode'=> 'flg'
]));
}
_die(json_encode([
'status'=> 'complete',
'PATH'=> getenv('PATH'),
'mode'=> 'cached'
]));
}
if($json['mode'] == 'get'){ // Get *.jp[e]g and *.png files list, for queue to converting
$images= [];
$cwebp= getBinary();
$time_limit_exception->enable();
recursive_search_img(BASE_PATH);
$ret= json_encode([
'status'=> 'complete',
'mode'=> 'get',
'images'=> $images,
'count'=> count($images),
'cwebp'=> $cwebp
]);
switch (json_last_error()) {
case JSON_ERROR_NONE:
_die($ret);
case JSON_ERROR_DEPTH:
_die(json_encode(['status'=> 'JSON_ERROR_DEPTH']));
case JSON_ERROR_STATE_MISMATCH:
_die(json_encode(['status'=> 'JSON_ERROR_STATE_MISMATCH']));
case JSON_ERROR_CTRL_CHAR:
_die(json_encode(['status'=> 'JSON_ERROR_CTRL_CHAR']));
case JSON_ERROR_SYNTAX:
_die(json_encode(['status'=> 'JSON_ERROR_SYNTAX']));
case JSON_ERROR_UTF8:
_die(json_encode(['status'=> 'JSON_ERROR_UTF8']));
default:
_die(json_encode(['status'=> 'JSON_ERROR']));
}
}
if($json['mode'] == 'convert'){ // Converting *.jp[e]g and *.png files to /webp/[*/]*.webp
if(isset($json['cwebp']) && $json['cwebp'] != 'gd'){
if($json['cwebp'] == 'system') {
$cwebp= false;
foreach(explode(':', getenv('PATH')) as $cwebp){
if(is_executable($cwebp.'/cwebp')) $cwebp= $cwebp.'/cwebp';
}
if(!$cwebp) _die(json_encode(['status'=> 'cwebp not found in system PATH: '. getenv('PATH')]));
} else {
if(is_file(__DIR__.DIRECTORY_SEPARATOR.'Binaries'.DIRECTORY_SEPARATOR.$json['cwebp']) ){
$cwebp= __DIR__.DIRECTORY_SEPARATOR.'Binaries'.DIRECTORY_SEPARATOR.$json['cwebp'];
} else {
_die(json_encode(['status'=> 'Wrong Bin file!']));
}
}
}
$dest= BASE_PATH.DIRECTORY_SEPARATOR.'webp'.$json['file'].'.webp';
$source= BASE_PATH.$json['file'];
$imagetype= exif_imagetype($source);
$gd_support= check_gd();
$return_var= 255;
$output= [];
if(!is_dir(dirname($dest))){
mkdir(dirname($dest), 0755, true);
}
if($imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG){
$output[]= "Fatal error, not supported source file format !!!";
}
if(is_file($source)){
if( is_file($dest) ){
if(filemtime($dest) > filemtime($source)) {
$output[]= "Info, destination file already converted.";
goto die_convert;
}
unlink($dest);
}
ignore_user_abort(true);
if($imagetype == IMAGETYPE_JPEG){
if($json['cwebp'] != 'gd') exec($cwebp.' '.$param_jpeg.' "'.$source.'" -o "'.$dest.'" 2>&1', $output, $return_var);
else $return_var= 1;
if( // Patch if error: Unsupported color conversion request, for YCCK JPGs
$return_var != 0 &&
$gd_support !== false &&
$gd_support['WebP Support'] == 1 &&
$gd_support['JPEG Support'] == 1
){
$return_var= gdConvert($source, $dest);
}
}
if($imagetype == IMAGETYPE_PNG){
if($json['cwebp'] != 'gd') exec($cwebp.' '.$param_png.' "'.$source.'" -o "'.$dest.'" 2>&1', $output, $return_var);
else $return_var= 1;
if( // Patch if error:
$return_var != 0 &&
$gd_support !== false &&
$gd_support['WebP Support'] == 1 &&
$gd_support['WebP Alpha Channel Support'] == 1 &&
$gd_support['PNG Support'] == 1
){
$return_var= gdConvert($source, $dest);
}
}
} else {
$return_var= 127;
$output[]= "Fatal error, source file not found !!!";
}
if(!is_file($dest)){
$output[]= "Fatal error, destination file not created !!!";
}
die_convert:
$ret= json_encode([
'status'=> 'complete',
'mode'=> 'convert',
'source'=> $json['file'],
'dest'=> DIRECTORY_SEPARATOR.'webp'.$json['file'].'.webp',
'output'=> $output,
'return_var'=> $return_var
]);
_die($ret);
}
////////////////////////////////////////////////////////////////////////
// Functions
function gdConvert($source, $dest){
global $output;
switch(exif_imagetype($source)){
case IMAGETYPE_JPEG:
$img= imagecreatefromjpeg($source);
break;
case IMAGETYPE_PNG:
$img= imagecreatefrompng($source);
imagepalettetotruecolor($img);
imagealphablending($img, true);
imagesavealpha($img, true);
break;
default:
return false;
}
$return_var= imageWebp($img, $dest, 75);
imagedestroy($img);
if($return_var && filesize($dest) % 2 == 1) { // No null byte at the end of the file
file_put_contents($dest, "\0", FILE_APPEND);
}
if($return_var){
//$return_var= 0;
$output[]= "Info, use PHP GD for convert image !";
}
return $return_var;
}
function getBinary(){ // Detect os and select converter command line tool
global $suppliedBinaries;
$gd_support= check_gd();
$gd= false;
if(
$gd_support !== false &&
$gd_support['WebP Support'] == 1 &&
$gd_support['WebP Alpha Channel Support'] == 1 &&
$gd_support['PNG Support'] == 1
){
$gd= true;
}
// Check disabled exec function
$disablefunc= explode(",", str_replace(" ", "", @ini_get("disable_functions")));
if(!is_callable("exec") || in_array("exec", $disablefunc)) {
if($gd) return "gd";
_die(json_encode(['status'=> 'Exec function disabled!']));
}
// https://github.com/rosell-dk/webp-convert
// https://developers.google.com/speed/webp/docs/precompiled
$cwebp_path= __DIR__.DIRECTORY_SEPARATOR.'Binaries'.DIRECTORY_SEPARATOR;
$return_var= 'Bin file for: '.PHP_OS.' not found in /connectors/converter/Binaries/';
$output= [];
if(strtolower(PHP_OS) == 'linux') {
foreach(explode(':', getenv('PATH')) as $cwebp){
if(is_executable($cwebp.'/cwebp')) {
$output[]= $cwebp.'/cwebp';
exec($cwebp.'/cwebp'.' 2>&1', $output, $return_var);
if($return_var == 0) {
return 'system';
}
}
}
}
if( !isset($suppliedBinaries[strtolower(PHP_OS)]) ) {
if($gd) return "gd";
_die(json_encode(['status'=> $return_var]));
}
$bin= $suppliedBinaries[strtolower(PHP_OS)]; // Select OS
if( is_array($bin) ){ // Check binary
foreach($bin as $b){
if( is_file($cwebp_path.$b) ){
if( !is_executable($cwebp_path.$b) ) chmod($cwebp_path.$b, 0755);
$output[]= "<hr />".$cwebp_path.$b;
exec($cwebp_path.$b.' 2>&1', $output, $return_var);
if( $return_var == 0){
$cwebp= $b;
break;
}
}
}
} else {
if( is_file($cwebp_path.$bin) ){
if( strtolower(PHP_OS) != 'winnt' && !is_executable($cwebp_path.$bin) ) chmod($cwebp_path.$bin, 0755);
$output[]= $cwebp_path.$bin;
exec($cwebp_path.$bin.' 2>&1', $output, $return_var);
if($return_var == 0) {
$cwebp= $bin;
}
}
}
if( !isset($cwebp) ) {
if(is_numeric($return_var)) {
if($gd) return "gd";
_die(json_encode([
'status'=> 'Bin file not work! return code: '.$return_var,
'mode'=> 'get_bin',
'output'=> $output,
'return_var'=> $return_var
]));
} else {
if($gd) return "gd";
_die(json_encode([
'status'=> $return_var,
'mode'=> 'get_bin',
'output'=> $output,
'return_var'=> 127
]));
}
}
// Download bin file from https://developers.google.com/speed/webp/docs/precompiled, into directory /connectors/converter/Binaries/
return $cwebp;
}
function recursive_search_img($dir){ // Search jpeg and png files recursive
$odir= opendir($dir);
while(($file= readdir($odir)) !== FALSE){
$full_path= $dir.DIRECTORY_SEPARATOR.$file;
if($file == '.' || $file == '..'){
continue;
}
if(is_dir($full_path)){
foreach($GLOBALS["excludeSubdirectories"] as $subdir){ // Exclude subdirectories from search
if(strpos($full_path, BASE_PATH.$subdir) !== false) continue 2;
}
recursive_search_img($full_path);
} else {
$ext= strtolower(pathinfo($file, PATHINFO_EXTENSION));
if(
$ext == 'jpg' ||
$ext == 'jpeg' ||
$ext == 'png'
){
$img= str_replace(BASE_PATH, '', $full_path);
$dest= BASE_PATH.DIRECTORY_SEPARATOR.'webp'.DIRECTORY_SEPARATOR.$img.'.webp';
if( is_file($dest) && filemtime($full_path) < filemtime($dest) ) continue;
$GLOBALS["images"][]= $img;
}
}
}
closedir($odir);
}
function recursive_search_webp($dir){ // Search webp files recursive
$odir= opendir($dir);
while(($file= readdir($odir)) !== FALSE){
if($file == '.' || $file == '..') continue;
$full_path= $dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($full_path)){
recursive_search_webp($full_path);
} else {
$ext= strtolower(pathinfo($file, PATHINFO_EXTENSION));
if($ext == 'webp'){
$dest= BASE_PATH.str_replace(
[BASE_PATH.DIRECTORY_SEPARATOR.'webp', '.webp'], '', $full_path
);
if( !is_file($dest) ) {
unlink($full_path);
}
}
}
}
closedir($odir);
}
function recursive_remove_empty_dirs($dir){ // Remove empty dirs
$odir= opendir($dir);
$count_files= 0;
while(($file= readdir($odir)) !== FALSE){
if($file == '.' || $file == '..') continue;
$count_files++;
if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
$count_files += recursive_remove_empty_dirs($dir.DIRECTORY_SEPARATOR.$file);
}
}
closedir($odir);
if($count_files == 0) {
rmdir( $dir );
return -1;
}
return $count_files;
}
function _die($return){
global $time_limit_exception;
$time_limit_exception->disable();
die($return);
}
function check_gd(){ // Prior to GD library version 2.2.5, WEBP does not have alpha channel support
if(extension_loaded('gd') && function_exists('gd_info') ){
$gd= gd_info();
if(!in_array('GD Version', $gd)) $gd['GD Version']= '0.0.0';
preg_match('/\\d+\\.\\d+(?:\\.\\d+)?/', $gd['GD Version'], $matches);
$gd['Ver']= $matches[0];
if(version_compare($gd['Ver'], '2.2.5') >= 0) {
$gd['WebP Alpha Channel Support']= 1;
} else {
$gd['WebP Alpha Channel Support']= 0;
}
if(!in_array('WebP Support', $gd)) $gd['WebP Support']= 0;
if(!in_array('JPEG Support', $gd)) $gd['JPEG Support']= 0;
if(!in_array('PNG Support', $gd)) $gd['PNG Support']= 0;
return $gd;
} else {
return false;
}
}
class time_limit_exception { // Exit if time exceed time_limit
protected $enabled= false;
public function __construct() {
register_shutdown_function( array($this, 'onShutdown') );
}
public function enable() {
$this->enabled= true;
}
public function disable() {
$this->enabled= false;
}
public function onShutdown() {
if ($this->enabled) { //Maximum execution time of $time_limit$ second exceeded
global $json;
if($json['mode'] == 'get'){ //Too many files, use SSD instead HDD, or try again several times hoping for the system cache of the file system
// Another solution, use autoconverter.py or image2webp.sh from https://github.com/commeta/autoconverter
global $images, $cwebp;
http_response_code(200);
if(isset($images) && isset($cwebp)){
$ret= json_encode([
'status'=> 'complete',
'mode'=> 'get',
'images'=> $images,
'count'=> count($images),
'cwebp'=> $cwebp,
'execution_time' => 'exceeded'
]);
if(json_last_error() != JSON_ERROR_NONE) _die(json_encode(['status'=> 'Wrong filenames encoding!']));
_die($ret);
}
}
}
}
}
?>