add resolution maximums
This commit is contained in:
parent
00d8e0b0a3
commit
c538a9acc0
|
@ -1,7 +1,11 @@
|
||||||
# imageoptimizer
|
# imageoptimizer
|
||||||
|
|
||||||
imageoptimizer is a tool for bulk compression og JPG images. It will re-compress original images and replace them if the savings is
|
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?
|
# why?
|
||||||
|
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module git.teamworkapps.com/shortcut/imageoptimizer
|
module git.teamworkapps.com/shortcut/imageoptimizer
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
|
require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
|
||||||
|
|
|
@ -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
12
main.go
|
@ -8,6 +8,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/nfnt/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
|
@ -18,6 +20,8 @@ type config struct {
|
||||||
StartingPath string
|
StartingPath string
|
||||||
Quiet bool
|
Quiet bool
|
||||||
AtLeast uint
|
AtLeast uint
|
||||||
|
MaxWidth uint
|
||||||
|
MaxHeight uint
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +40,8 @@ func main() {
|
||||||
flag.UintVar(&c.Quality, "quality", 85, "JPG compression quality level.")
|
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.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.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.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.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()
|
flag.Parse()
|
||||||
|
@ -123,6 +129,12 @@ func doSingleImage(f string, c config, s stats) stats {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
in.Close()
|
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())
|
out, err := os.OpenFile("imageoptimizer.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, st.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Couldn't open out temprary file: %s\n", err.Error())
|
fmt.Printf("Couldn't open out temprary file: %s\n", err.Error())
|
||||||
|
|
Loading…
Reference in New Issue