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