add resolution maximums

This commit is contained in:
Your Name 2022-02-02 11:37:47 -05:00
parent 00d8e0b0a3
commit c538a9acc0
4 changed files with 21 additions and 1 deletions

View File

@ -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?

2
go.mod
View File

@ -1,3 +1,5 @@
module git.teamworkapps.com/shortcut/imageoptimizer
go 1.17
require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect

2
go.sum Normal file
View File

@ -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=

12
main.go
View File

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