-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressimages.php
85 lines (71 loc) · 2.8 KB
/
compressimages.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
<html>
<head>
<title>Compress JPEG Images for Vanilla Forum</title>
</head>
<body>
<?php
// CONFIGURE HERE
$domain = 'https://jgauroraforum.com/';
$startdir=getcwd().'/uploads';
function get_extension($filename) {
$splitfilename = explode(".", $filename);
$extension = end($splitfilename);
return $extension ? $extension : false;
}
function listFolderFiles($dir,$startdir,$manualselect,$domain){
$imgextarray = array("jpg", "jpeg", "png", "gif");
$largeimgsize = 800000;
$fileisimg = 0;
$imgoversizetargetratio = 1.2;
$ffs = scandir($dir);
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
// prevent empty ordered elements
if (count($ffs) < 1)
return;
echo '<ol>';
foreach($ffs as $ff){
$isdir = is_dir($dir.'/'.$ff);
if($isdir){
//echo '<li>'.$ff.' '.$dir.'</li>';
listFolderFiles($dir.'/'.$ff,$startdir,$manualselect,$domain);
}
else {
$fileext = get_extension($ff);
if (in_array($fileext,$imgextarray)) {
$fileisimg = 1;
$imgfilepath = $dir.'/'.$ff;
$uploadpath =substr($dir,strlen($startdir)).'/'.$ff;
$imgurl = $domain.'uploads'.$uploadpath;
$imgfilesize=filesize($imgfilepath);
if ($imgfilesize > $largeimgsize ) {
$imgsizedetails=getimagesize($imgfilepath);
$estimgsize = $imgsizedetails[0]*$imgsizedetails[1]/8;
$imgoversizeratio = $imgfilesize/$estimgsize;
if ($imgoversizeratio > $imgoversizetargetratio){
if ($fileext=='jpg' || $fileext=='jpeg'){
if ( $manualselect == 1){
echo '<li>'.'<a href=\''.$imgurl.'\'>'.$ff.'</a> , size='.$imgfilesize. ' bytes, ratio='.$imgoversizeratio.' compressed=<a href=\''.$domain.'compressimage.php?autodelete=1&path='.$uploadpath.'\'>click</a></li>';
}else{
if (strpos($ff, "_originalimg.".$fileext) == true) {
unlink($imgfilepath) or die("Couldn't delete file");
}
else {
header('Location: '.$domain.'compressimage.php?autodelete=1&path='.$uploadpath);
die();
}
}
}
}
}
}
}
}
echo '</ol>';
}
$manualselect = (int)$_GET["manual"];
if ($manualselect == 1){echo "Running in manual mode. ";}
listFolderFiles($startdir,$startdir,$manualselect,$domain);
?>
</body>
</html>