-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_img.php
55 lines (51 loc) · 1.7 KB
/
optimize_img.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
<?php
/**
* PHP cli script that optimizes images (jpg and png) recursive in a directory
*
* <code>
* $ php optimize_img.php
* </code>
*
* Before using it make sure to install optipng and jpegtran. On Debian/Ubuntu that would be:
*
* <code>
* $ sudo apt-get install libjpeg-progs optipng
* </code>
*
* @version $Id: optimize_img.php,v 1.00 2009/11/18 10:54:32 $
* @copyright Copyright (c) 2014 Nick Papanotas (http://twitter.com/HumanWorks)
* @author Nick Papanotas <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*
*/
define('DIRECTORY', dirname(__FILE__));
function GetDirContents($dir){
if (!is_dir($dir)){die ("Function GetDirContents: Problem reading : $dir!");}
if ($root=@opendir($dir)){
while ($file=readdir($root)){
if($file=="." || $file==".."){continue;}
if(is_dir($dir."/".$file)){
$files=@array_merge($files,GetDirContents($dir."/".$file));
}else{
$files[]=$dir."/".$file;
}
}
}
return $files;
}
foreach(GetDirContents(DIRECTORY) as $f){
$ext = strtolower(array_pop(explode('.', $f)));
if ($ext == 'jpg' || $ext == 'png') {
echo $f, "\n";
$size = filesize($f);
if ( $ext == 'jpg')
exec( 'jpegtran -copy none -optimize -outfile '. $f . ' ' . $f );
else
exec( 'optipng ' . $f );
clearstatcache();
$size_new = filesize($f);
$perc = 100 - ($size_new/$size ) * 100;
echo 'Original size: ', $size, ' bytes, New size: ', $size_new, ' bytes.', "\nReduced by: ", number_format($perc,2), "%\n";
}
}
?>