-
Notifications
You must be signed in to change notification settings - Fork 0
/
psd_optimize.php
69 lines (55 loc) · 2.19 KB
/
psd_optimize.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
#!/usr/bin/php
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
define("MAX_IMG_HEIGHT", 2048);
define("MAX_IMG_WIDTH", 1800);
define("MOZCJPEG_QUALITY", 70);
define("MIN_FILESIZE", 100000); // 100kb in bytes
define("MIN_COLORS", 16);
$script_dir = getcwd();
$convert_path = trim(`which convert`); // for debugging ImageMagick issues
// check for all required arguments
// first argument is always name of script!
if ($argc < 2) {
die("\nUsage: php psd_optimize.php <\"path to PSD or PNG file\">\n\n");
}
// remove first argument
array_shift($argv);
// get and use remaining arguments
$image = trim( $argv[0] );
$image = str_replace('\\','', $image);
$ext = strtolower( substr($image, -3) );
$fsize = filesize($image);
if( $ext != 'psd' && $ext != 'png' ) {
die("\nERROR: Input file must be a PSD or PNG file!\n\n");
}
$final_image = substr($image, 0, -4) . '_OPTIMIZED.png';
// if image is a PSD, we only want the visible layers
if($ext == 'psd') {
$image = $image . '[0]';
}
// first, is there transparency? If not, we can potentially convert to JPEG
$command = "identify -format '%[opaque]' \"$image\"";
$opaque = `$command`;
// next, how many colors?
$command = "identify -format '%k' \"$image\"";
$colors = `$command`;
echo "\nOPAQUE: $opaque\nCOLORS: $colors\n";
// if conditions are met, save as a JPEG
if(stripos($opaque, 'true') > -1 && trim($colors) >= MIN_COLORS && $fsize > MIN_FILESIZE) {
echo "Saving as JPEG\n";
$final_image = substr($final_image, 0, -3) . 'jpg';
$command = "convert \"$image\" -verbose -strip -resize ". MAX_IMG_WIDTH ."x". MAX_IMG_HEIGHT ."\> pnm:- | mozcjpeg -quality ".MOZCJPEG_QUALITY." > \"$final_image\"";
`$command`;
} else { // save as PNG
echo "Saving as PNG\n";
$command = "convert \"$image\" -verbose -strip -resize ". MAX_IMG_WIDTH ."x". MAX_IMG_HEIGHT ."\> \"$final_image\"";
`$command`;
$command = "pngquant --quality=50-70 --speed=1 --force --skip-if-larger --verbose --output \"$final_image\" \"$final_image\"";
`$command`;
$command = "optipng -clobber -verbose -fix -strip all \"$final_image\"";
`$command`;
}
echo "\n############################# OPTIMIZATION COMPLETE #############################\n\n";
echo "PATH TO OPTIMIZED IMAGE: $final_image\n\n";
?>