Update flags

This commit is contained in:
Ralph Slooten 2019-08-03 00:37:51 +12:00
parent 3a09a2e666
commit 2af6ad5566
2 changed files with 20 additions and 19 deletions

View File

@ -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)
}
}

27
main.go
View File

@ -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] <images>\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 <width>x<height>.")
flag.BoolVar(&preserveModificationTimes, "p", true, "Preserve file modification times")
flag.StringVar(&maxSizes, "m", "", "Downscale to a maximum width & height in pixels (<width>x<height>)")
// third-party optimizers
flag.StringVar(&gifsicle, "gifsicle", "gifsicle", "gifsicle binary")