From c538a9acc0c150cad0edad0322aec883acfaf145 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 2 Feb 2022 11:37:47 -0500 Subject: [PATCH] add resolution maximums --- README.md | 6 +++++- go.mod | 2 ++ go.sum | 2 ++ main.go | 12 ++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 go.sum diff --git a/README.md b/README.md index 4f4c744..7c4b141 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@ # imageoptimizer imageoptimizer is a tool for bulk compression og JPG images. It will re-compress original images and replace them if the savings is -more than the requested percentage difference and optionally if the original file is bigger than a cefrtain size. By default it operates in a "dry run" mode where images are not replaced but individual and total savings are reported at the end, unless you supply the '-replace' flag. +more than the requested percentage difference and optionally if the original file is bigger than a cefrtain size. You can also +set a maximum resolution for images and they will be rescaled if they exceed it, however they are only replaced if they meet the size +difference requirement set. + +By default it operates in a "dry run" mode where images are not replaced but individual and total savings are reported at the end, unless you supply the '-replace' flag. # why? diff --git a/go.mod b/go.mod index 59e5cfb..04e0ecb 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.teamworkapps.com/shortcut/imageoptimizer go 1.17 + +require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..96adbed --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= diff --git a/main.go b/main.go index 24c26bd..851681e 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/nfnt/resize" ) type config struct { @@ -18,6 +20,8 @@ type config struct { StartingPath string Quiet bool AtLeast uint + MaxWidth uint + MaxHeight uint } @@ -36,6 +40,8 @@ func main() { flag.UintVar(&c.Quality, "quality", 85, "JPG compression quality level.") flag.BoolVar(&c.Quiet, "quiet", false, "Less output - don't print per-file detail") flag.UintVar(&c.AtLeast, "atleast", 0, "Ignore images that aren't at least this many kilobytes") + flag.UintVar(&c.MaxWidth, "maxwidth", 0, "Maximum width, scale images bigger to fit within this width. 0 ignores. Won't replace unless it meets the diff threashold.") + flag.UintVar(&c.MaxHeight, "maxheight", 0, "Maximum height, scale images bigger to fit within this height. 0 ignores. Won't replace until it meets the diff threashold.") flag.StringVar(&c.StartingPath, "startingpath", ".", "Start from this path instead of current working dir") flag.BoolVar(&c.Replace, "replace", false, "Replace with compressed versions if criteria are met. Otheriwse, just test and report, don't replace any images. (default false)") flag.Parse() @@ -123,6 +129,12 @@ func doSingleImage(f string, c config, s stats) stats { return s } in.Close() + // Check bounds and resize if necessary + ores:=orig.Bounds() + if (c.MaxWidth !=0 && ores.Dx() > int(c.MaxWidth) ) || (c.MaxHeight!=0 && ores.Dy()>int(c.MaxHeight)) { + orig=resize.Thumbnail(c.MaxWidth, c.MaxHeight, orig, resize.Lanczos3) + fmt.Printf("Resized from %dx%d - ", ores.Dx(), ores.Dy()) + } out, err := os.OpenFile("imageoptimizer.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, st.Mode()) if err != nil { fmt.Printf("Couldn't open out temprary file: %s\n", err.Error())