diff --git a/goptimize.go b/goptimize.go index 9648830..17ee5bf 100644 --- a/goptimize.go +++ b/goptimize.go @@ -154,7 +154,7 @@ func Goptimize(file string) { // calculate saved percent savedPercent := 100 - math.Round(float64(dstSize)/float64(srcSize)*100) - if dstSize < srcSize { + if savedPercent > 0 { // (over)write the file - not all filesystems support // cross-filesystem moving so we overwrite the original out, err := os.Create(dstFile) @@ -170,14 +170,14 @@ func Goptimize(file string) { return } - if !skipPreserveModTimes { + if preserveModificationTimes { // transfer original modification times if err := os.Chtimes(dstFile, atime, mtime); err != nil { fmt.Printf("Error setting file timestamp: %v\n", err) } } - fmt.Printf("Goptimized %s (%dx%d %s->%s %v%%)\n", dstFile, resultW, resultH, ByteCountSI(srcSize), ByteCountSI(dstSize), savedPercent) + fmt.Printf("Goptimized %s (%dx%d %s > %s %v%%)\n", dstFile, resultW, resultH, ByteCountSI(srcSize), ByteCountSI(dstSize), savedPercent) } else { // If the output directory is not the same, // then write a copy of the original file @@ -199,17 +199,17 @@ func Goptimize(file string) { return } - if !skipPreserveModTimes { + if preserveModificationTimes { // transfer original modification times if err := os.Chtimes(dstFile, atime, mtime); err != nil { fmt.Printf("Error setting file timestamp: %v\n", err) } } - fmt.Printf("Copied %s (%dx%d %s->%s %v%%)\n", dstFile, srcW, srcH, ByteCountSI(srcSize), ByteCountSI(srcSize), 0) + fmt.Printf("Copied %s (%dx%d %s %v%%)\n", dstFile, srcW, srcH, ByteCountSI(srcSize), 0) } else { // we didn't actually change anything - fmt.Printf("Skipped %s (%dx%d %s->%s %v%%)\n", dstFile, srcW, srcH, ByteCountSI(srcSize), ByteCountSI(srcSize), 0) + fmt.Printf("Skipped %s (%dx%d %s %v%%)\n", dstFile, srcW, srcH, ByteCountSI(srcSize), 0) } } diff --git a/main.go b/main.go index d45c565..0a202f7 100644 --- a/main.go +++ b/main.go @@ -10,26 +10,27 @@ import ( ) var ( - quality int - maxWidth int - maxHeight int - outputDir string - skipPreserveModTimes bool - jpegoptim string - jpegtran string - optipng string - pngquant string - gifsicle string + quality int + maxWidth int + maxHeight int + outputDir string + preserveModificationTimes bool + jpegoptim string + jpegtran string + optipng string + pngquant string + gifsicle string ) func main() { // set the default help flag.Usage = func() { - fmt.Println("Goptimize - Resample optimized images") + fmt.Println("Goptimize - downscales and optimizes existing images") fmt.Printf("\nUsage: %s [options] \n", os.Args[0]) fmt.Println("\nOptions:") flag.PrintDefaults() fmt.Println("\nExamples:") + fmt.Printf(" %s image.png\n", os.Args[0]) fmt.Printf(" %s -m 800x800 *.jpg\n", os.Args[0]) fmt.Printf(" %s -o out/ -q 90 -m 1600x1600 *.jpg\n", os.Args[0]) @@ -46,8 +47,8 @@ func main() { flag.IntVar(&quality, "q", 75, "Quality - JPEG only") flag.StringVar(&outputDir, "o", "", "Output directory (default overwrites original)") - flag.BoolVar(&skipPreserveModTimes, "n", false, "Do not preserve file modification times") - flag.StringVar(&maxSizes, "m", "", "Scale down to a maximum width & height. Format must be x.") + flag.BoolVar(&preserveModificationTimes, "p", true, "Preserve file modification times") + flag.StringVar(&maxSizes, "m", "", "Downscale to a maximum width & height in pixels (x)") // third-party optimizers flag.StringVar(&gifsicle, "gifsicle", "gifsicle", "gifsicle binary")