├── .gitignore
├── go.mod
├── go.sum
├── core
├── png.go
├── gif.go
├── bmp.go
├── jpeg.go
├── webp.go
├── tiff.go
└── file.go
├── README.md
├── Makefile
├── main.go
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | build
2 | .vscode
3 | .idea
4 | .DS_Store
5 | goreleaser.yaml
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module ImgResizer
2 |
3 | go 1.16
4 |
5 | require (
6 | github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
7 | golang.org/x/image v0.0.0-20220617043117-41969df76e82
8 | )
9 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
2 | github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
3 | golang.org/x/image v0.0.0-20220617043117-41969df76e82 h1:KpZB5pUSBvrHltNEdK/tw0xlPeD13M6M6aGP32gKqiw=
4 | golang.org/x/image v0.0.0-20220617043117-41969df76e82/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY=
5 | golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
6 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
7 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
8 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
9 |
--------------------------------------------------------------------------------
/core/png.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "image/png"
15 | "os"
16 | )
17 |
18 | func PngEncode(source string) (image.Image, error) {
19 | file, err := os.Open(source)
20 | if err != nil {
21 | fmt.Printf("Error opening PNG file %s, %v", source, err)
22 | return nil, err
23 | }
24 | defer file.Close()
25 |
26 | return png.Decode(file)
27 | }
28 |
29 | func PngDecode(dest string, image image.Image) error {
30 | out, err := os.Create(dest)
31 | if err != nil {
32 | fmt.Printf("Error creating PNG file %s, %v", dest, err)
33 | return err
34 | }
35 | defer out.Close()
36 |
37 | return png.Encode(out, image)
38 | }
39 |
--------------------------------------------------------------------------------
/core/gif.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "image/gif"
15 | "os"
16 | )
17 |
18 | func GifEncode(source string) (image.Image, error) {
19 | file, err := os.Open(source)
20 | if err != nil {
21 | fmt.Printf("Error opening GIF file %s, %v", source, err)
22 | return nil, err
23 | }
24 | defer file.Close()
25 |
26 | return gif.Decode(file)
27 | }
28 |
29 | func GifDecode(dest string, image image.Image) error {
30 | out, err := os.Create(dest)
31 | if err != nil {
32 | fmt.Printf("Error creating GIF file %s, %v", dest, err)
33 | return err
34 | }
35 | defer out.Close()
36 |
37 | return gif.Encode(out, image, nil)
38 | }
39 |
--------------------------------------------------------------------------------
/core/bmp.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "os"
15 |
16 | "golang.org/x/image/bmp"
17 | )
18 |
19 | func BmpEncode(source string) (image.Image, error) {
20 | file, err := os.Open(source)
21 | if err != nil {
22 | fmt.Printf("Error opening BMP file %s, %v", source, err)
23 | return nil, err
24 | }
25 | defer file.Close()
26 |
27 | return bmp.Decode(file)
28 | }
29 |
30 | func BmpDecode(dest string, image image.Image) error {
31 | out, err := os.Create(dest)
32 | if err != nil {
33 | fmt.Printf("Error creating BMP file %s, %v", dest, err)
34 | return err
35 | }
36 | defer out.Close()
37 |
38 | return bmp.Encode(out, image)
39 | }
40 |
--------------------------------------------------------------------------------
/core/jpeg.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "image/jpeg"
15 | "os"
16 | )
17 |
18 | func JpegEncode(source string) (image.Image, error) {
19 | file, err := os.Open(source)
20 | if err != nil {
21 | fmt.Printf("Error opening JPG file %s, %v", source, err)
22 | return nil, err
23 | }
24 | defer file.Close()
25 |
26 | return jpeg.Decode(file)
27 | }
28 |
29 | func JpegDecode(dest string, image image.Image) error {
30 | out, err := os.Create(dest)
31 | if err != nil {
32 | fmt.Printf("Error creating JPG file %s, %v", dest, err)
33 | return err
34 | }
35 | defer out.Close()
36 |
37 | return jpeg.Encode(out, image, &jpeg.Options{Quality: jpeg.DefaultQuality})
38 | }
39 |
--------------------------------------------------------------------------------
/core/webp.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "image/png"
15 | "os"
16 |
17 | "golang.org/x/image/webp"
18 | )
19 |
20 | func WebpEncode(source string) (image.Image, error) {
21 | file, err := os.Open(source)
22 | if err != nil {
23 | fmt.Printf("Error opening WEBP file %s, %v", source, err)
24 | return nil, err
25 | }
26 | defer file.Close()
27 |
28 | return webp.Decode(file)
29 | }
30 |
31 | func WebpDecode(dest string, image image.Image) error {
32 | out, err := os.Create(dest)
33 | if err != nil {
34 | fmt.Printf("Error creating PNG(Converted from WEBP) file %s, %v", dest, err)
35 | return err
36 | }
37 | defer out.Close()
38 |
39 | return png.Encode(out, image)
40 | }
41 |
--------------------------------------------------------------------------------
/core/tiff.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "os"
15 |
16 | "golang.org/x/image/tiff"
17 | )
18 |
19 | func TiffEncode(source string) (image.Image, error) {
20 | file, err := os.Open(source)
21 | if err != nil {
22 | fmt.Printf("Error opening TIFF file %s, %v", source, err)
23 | return nil, err
24 | }
25 | defer file.Close()
26 |
27 | return tiff.Decode(file)
28 | }
29 |
30 | func TiffDecode(dest string, image image.Image) error {
31 | out, err := os.Create(dest)
32 | if err != nil {
33 | fmt.Printf("Error creating TIFF file %s, %v", dest, err)
34 | return err
35 | }
36 | defer out.Close()
37 |
38 | return tiff.Encode(out, image, &tiff.Options{Compression: tiff.Uncompressed})
39 | }
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ImgResizer
2 |
3 | 批量图片等比缩放、类型转换工具
4 | 1. 支持图片类型:bmp、tiff、jpg、jpeg、gif、png、webp
5 | 1. 支持类型转换为:bmp、tiff、jpg、jpeg、gif、png
6 | 1. 支持自定义宽度、高度
7 | 1. 五种等比缩放模式
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | ## 使用方法
19 |
20 | ```
21 | ImgResizer -source {source} -dest {dest} -mode {mode}
22 | -dest string
23 | Destination file or directory
24 | -format string
25 | Output format
26 | Supported values: png|jpg|jpeg|bmp|tiff|gif
27 | Omit to keep original format
28 | -height int
29 | Destination height
30 | Omit to keep original height (default -1)
31 | -help
32 | Show help message
33 | -mode int
34 | 0 - (Default) Nearest-neighbor interpolation
35 | 1 - Bilinear interpolation
36 | 2 - Bicubic interpolation
37 | 3 - Mitchell-Netravali interpolation
38 | 4 - Lanczos resampling with a=2
39 | 5 - Lanczos resampling with a=3
40 | -source string
41 | Source file or directory
42 | -width int
43 | Destination width
44 | Omit to keep original width (default -1)
45 | ```
46 |
47 | ## 注意事项
48 |
49 | 1. **如果不需要改变原图类型,请省略 `-format` 参数**
50 | 1. **webp 格式图片,默认转换为 png 格式处理(目前没有 webp 图片的高效、简洁处理办法)**
51 | 1. **如果不需要改变原图尺寸,请同时省略 `-width` 和 `-height` 参数**
52 |
53 | ## 使用示例
54 |
55 | ### 1. 批量等比缩放
56 |
57 | ```
58 | ImgResizer -source ~/pics -dest ~/new_pics -mode 5 -height 128 -width 300
59 | ```
60 |
61 | ### 2. 单文件指定宽度
62 |
63 | ```
64 | ImgResizer -source ~/pics/hello.gif -dest ~/newpics/wow.gif -width 900
65 | ```
66 |
67 | ### 3. 批量类型转换
68 |
69 | ```
70 | ImgResizer -source ~/pics -dest ~/new_pics -format jpg
71 | ```
72 |
73 | ## ImgResizer
74 | 1. Gitee [https://gitee.com/barat/imgresizer](https://gitee.com/barat/imgresizer)
75 | 1. Github [https://github.com/barats/ImgResizer](https://github.com/barats/ImgResizer)
76 | 1. Gitlink [https://www.gitlink.org.cn/baladiwei/imgresizer](https://www.gitlink.org.cn/baladiwei/imgresizer)
77 | 1. 收录信息 [https://www.oschina.net/p/ImgResizer](https://www.oschina.net/p/ImgResizer)
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: all build compress build-macos build-linux build-windows compress-macos compress-linux compress-windows help
2 |
3 | BUILD_PATH=./build
4 | APPNAME=ImgResizer
5 |
6 | all: build compress
7 | build: build-macos build-linux build-windows
8 | compress: compress-macos compress-linux compress-windows
9 |
10 | build-macos:
11 | @echo "开始 macOS 可执行程序编译..."
12 | CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o $(BUILD_PATH)/$(APPNAME)-darwin-x86_64
13 | CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o $(BUILD_PATH)/$(APPNAME)-darwin-arm64
14 | @echo "macOS 可执行程序编译完成..."
15 |
16 | build-linux:
17 | @echo "开始 Linux 可执行程序编译..."
18 | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o $(BUILD_PATH)/$(APPNAME)-linux-x86_64
19 | CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o $(BUILD_PATH)/$(APPNAME)-linux-arm64
20 | @echo "Linux 可执行程序编译完成..."
21 |
22 | build-windows:
23 | @echo "开始 Windows 可执行程序编译..."
24 | CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o $(BUILD_PATH)/$(APPNAME)-windows-x86_64.exe
25 | @echo "Windows 可执行程序编译完成..."
26 |
27 | compress-macos:
28 | @echo "macOS 可执行程序压缩开始..."
29 | mv $(BUILD_PATH)/$(APPNAME)-darwin-x86_64 $(BUILD_PATH)/$(APPNAME)-darwin-x86_64_tmp
30 | upx --best -o $(BUILD_PATH)/$(APPNAME)-darwin-x86_64 $(BUILD_PATH)/$(APPNAME)-darwin-x86_64_tmp
31 | rm -fr $(BUILD_PATH)/$(APPNAME)-darwin-x86_64_tmp
32 | mv $(BUILD_PATH)/$(APPNAME)-darwin-arm64 $(BUILD_PATH)/$(APPNAME)-darwin-arm64_tmp
33 | upx --best -o $(BUILD_PATH)/$(APPNAME)-darwin-arm64 $(BUILD_PATH)/$(APPNAME)-darwin-arm64_tmp
34 | rm -fr $(BUILD_PATH)/$(APPNAME)-darwin-arm64_tmp
35 | @echo "macOS 可执行程序压缩完成..."
36 |
37 | compress-linux:
38 | @echo "Linux 可执行程序压缩开始..."
39 | mv $(BUILD_PATH)/$(APPNAME)-linux-x86_64 $(BUILD_PATH)/$(APPNAME)-linux-x86_64_tmp
40 | upx --best -o $(BUILD_PATH)/$(APPNAME)-linux-x86_64 $(BUILD_PATH)/$(APPNAME)-linux-x86_64_tmp
41 | rm -fr $(BUILD_PATH)/$(APPNAME)-linux-x86_64_tmp
42 | mv $(BUILD_PATH)/$(APPNAME)-linux-arm64 $(BUILD_PATH)/$(APPNAME)-linux-arm64_tmp
43 | upx --best -o $(BUILD_PATH)/$(APPNAME)-linux-arm64 $(BUILD_PATH)/$(APPNAME)-linux-arm64_tmp
44 | rm -fr $(BUILD_PATH)/$(APPNAME)-linux-arm64_tmp
45 | @echo "Linux 可执行程序压缩完成..."
46 |
47 | compress-windows:
48 | @echo "Windows 可执行程序压缩开始..."
49 | mv $(BUILD_PATH)/$(APPNAME)-windows-x86_64.exe $(BUILD_PATH)/$(APPNAME)-windows-x86_64.exe_tmp
50 | upx --best -o $(BUILD_PATH)/$(APPNAME)-windows-x86_64.exe $(BUILD_PATH)/$(APPNAME)-windows-x86_64.exe_tmp
51 | rm -fr $(BUILD_PATH)/$(APPNAME)-windows-x86_64.exe_tmp
52 | @echo "Windows 可执行程序压缩完成..."
53 |
54 | help:
55 | @echo "ImgResizer 构建命令集,用于快速构建服务。"
56 | @echo ""
57 | @echo "用法:"
58 | @echo ""
59 | @echo " make "
60 | @echo ""
61 | @echo "可用命令如下::"
62 | @echo " make all 格式化代码, 编译生成并压缩全部平台的可执行程序"
63 | @echo " make build 编译生成全部可执行程序"
64 | @echo " make compress 压缩全部可执行程序"
65 | @echo " make build-macos 构建 masOS 可执行程序"
66 | @echo " make build-linux 构建 Linux 可执行程序"
67 | @echo " make build-windows 构建 Windows 可执行程序"
68 | @echo " make compress-macos 压缩 masOS 可执行程序"
69 | @echo " make compress-linux 压缩 Linux 可执行程序"
70 | @echo " make compress-windows 压缩 Windows 可执行程序"
71 | @echo " make help 查看帮助"
72 |
73 |
74 |
--------------------------------------------------------------------------------
/core/file.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package core
10 |
11 | import (
12 | "fmt"
13 | "image"
14 | "io"
15 | "os"
16 | "strings"
17 |
18 | "github.com/nfnt/resize"
19 | )
20 |
21 | type OutputOptions struct {
22 | Width int
23 | Height int
24 | Interpolation resize.InterpolationFunction
25 | DestPath string
26 | Format OutputFormat
27 | }
28 |
29 | type OutputFormat string
30 |
31 | const (
32 | PNG OutputFormat = "png"
33 | JPG OutputFormat = "jpg"
34 | JPEG OutputFormat = "jpeg"
35 | BMP OutputFormat = "bmp"
36 | TIFF OutputFormat = "tiff"
37 | GIF OutputFormat = "gif"
38 | )
39 |
40 | //
41 | //Deal with image files
42 | func DealWithFile(source string, option OutputOptions) error {
43 | file, err := os.Open(source)
44 | if err != nil {
45 | fmt.Printf("Error opening file %s, %v", source, err)
46 | return err
47 | }
48 | defer file.Close()
49 |
50 | fileFormat, width, height, err := retrieveImageInfo(file)
51 | if err != nil {
52 | fmt.Printf("Could not guess mime type of file %s, %v", source, err)
53 | return err
54 | }
55 |
56 | file.Seek(0, 0) //MUST SEEK BACK TO 0,0 acording to https://github.com/golang/go/issues/50992
57 |
58 | var data image.Image
59 | //encode image file for different type
60 | switch fileFormat {
61 | case "bmp":
62 | data, err = BmpEncode(source)
63 | case "jpg":
64 | data, err = JpegEncode(source)
65 | case "jpeg":
66 | data, err = JpegEncode(source)
67 | case "tiff":
68 | data, err = TiffEncode(source)
69 | case "webp":
70 | data, err = WebpEncode(source)
71 | case "png":
72 | data, err = PngEncode(source)
73 | case "gif":
74 | data, err = GifEncode(source)
75 | default:
76 | err = fmt.Errorf("unsupported image type %s", fileFormat)
77 | }
78 |
79 | if err != nil {
80 | return err
81 | }
82 |
83 | if option.Width == -1 {
84 | option.Width = width
85 | }
86 |
87 | if option.Height == -1 {
88 | option.Height = height
89 | }
90 |
91 | afterResize := resize.Resize(uint(option.Width), uint(option.Height), data, option.Interpolation)
92 |
93 | if strings.EqualFold("", string(option.Format)) {
94 | option.Format = OutputFormat(fileFormat)
95 | }
96 |
97 | switch option.Format {
98 | case "bmp":
99 | err = BmpDecode(fmt.Sprintf("%s_%d_%d.bmp", strings.TrimSpace(option.DestPath), option.Width, option.Height), afterResize)
100 | case "jpg":
101 | err = JpegDecode(fmt.Sprintf("%s_%d_%d.jpg", strings.TrimSpace(option.DestPath), option.Width, option.Height), afterResize)
102 | case "jpeg":
103 | err = JpegDecode(fmt.Sprintf("%s_%d_%d.jpeg", strings.TrimSpace(option.DestPath), option.Width, option.Height), afterResize)
104 | case "tiff":
105 | err = TiffDecode(fmt.Sprintf("%s_%d_%d.tiff", strings.TrimSpace(option.DestPath), option.Width, option.Height), afterResize)
106 | case "gif":
107 | err = GifDecode(fmt.Sprintf("%s_%d_%d.gif", strings.TrimSpace(option.DestPath), option.Width, option.Height), afterResize)
108 | case "webp":
109 | case "png":
110 | err = PngDecode(fmt.Sprintf("%s_%d_%d.png", strings.TrimSpace(option.DestPath), option.Width, option.Height), afterResize)
111 | default:
112 | err = fmt.Errorf("unsupported output format %s", option.Format)
113 | }
114 |
115 | return err
116 | } //end of function
117 |
118 | //
119 | //Guess image type
120 | func retrieveImageInfo(r io.Reader) (string, int, int, error) {
121 | config, format, err := image.DecodeConfig(r)
122 | if err != nil {
123 | return "", -1, -1, err
124 | }
125 |
126 | return format, config.Width, config.Height, nil
127 | }
128 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | //Copyright (c) [2022] [巴拉迪维]
2 | //[ImgResizer] is licensed under Mulan PSL v2.
3 | //You can use this software according to the terms and conditions of the Mulan PSL v2.
4 | //You may obtain a copy of Mulan PSL v2 at:
5 | //http://license.coscl.org.cn/MulanPSL2
6 | //THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
7 | //See the Mulan PSL v2 for more details.
8 |
9 | package main
10 |
11 | import (
12 | "ImgResizer/core"
13 | "flag"
14 | "fmt"
15 | "io/ioutil"
16 | "os"
17 | "path/filepath"
18 | "strings"
19 |
20 | "github.com/nfnt/resize"
21 | )
22 |
23 | const Version string = "v1.3"
24 |
25 | var (
26 | cmdSource string
27 | cmdDest string
28 | cmdResizeMode int
29 | cmdWidth int
30 | cmdHeight int
31 | cmdHelp bool
32 | cmdFormat string
33 | )
34 |
35 | func init() {
36 | flag.StringVar(&cmdFormat, "format", "", "Output format \nSupported values: png|jpg|jpeg|bmp|tiff|gif \nOmit to keep original format ")
37 | flag.BoolVar(&cmdHelp, "help", false, "Show help message ")
38 | flag.IntVar(&cmdWidth, "width", -1, "Destination width \nOmit to keep original width")
39 | flag.IntVar(&cmdHeight, "height", -1, "Destination height \nOmit to keep original height")
40 | flag.StringVar(&cmdSource, "source", "", "Source file or directory")
41 | flag.StringVar(&cmdDest, "dest", "", "Destination file or directory")
42 | flag.IntVar(&cmdResizeMode, "mode", 0, `0 - (Default) Nearest-neighbor interpolation
43 | 1 - Bilinear interpolation
44 | 2 - Bicubic interpolation
45 | 3 - Mitchell-Netravali interpolation
46 | 4 - Lanczos resampling with a=2
47 | 5 - Lanczos resampling with a=3`)
48 |
49 | flag.Usage = func() {
50 | fmt.Printf("Usage of ImgResizer %s\nFor more information, please visit: \nhttps://github.com/barats/ImgResizer or https://gitee.com/barat/imgresizer \n\nImgResizer -source {source} -dest {dest} -mode {mode}\n", Version)
51 | flag.PrintDefaults()
52 | }
53 | }
54 |
55 | func main() {
56 |
57 | flag.Parse()
58 |
59 | if cmdHelp {
60 | flag.Usage()
61 | return
62 | }
63 |
64 | if strings.EqualFold("", strings.TrimSpace(cmdSource)) || strings.EqualFold("", strings.TrimSpace(cmdDest)) {
65 | fmt.Println("Missing parameter <-source> or <-dest>. Please -h or -help to show help message.")
66 | return
67 | }
68 |
69 | sourceInfo, err := os.Stat(cmdSource)
70 | if err != nil {
71 | fmt.Printf("Cant not open %s, error %v", cmdSource, err)
72 | return
73 | }
74 |
75 | if sourceInfo.IsDir() {
76 | //Assume that source & destination are directories which include image files in it
77 | //Assume that destination directory is existed(create if it's not)
78 | //Assume that destination directory is empty(override if it's not)
79 | files, err := ioutil.ReadDir(cmdSource)
80 | if err != nil {
81 | fmt.Printf("Error reading directory %s, %v", cmdSource, err)
82 | return
83 | }
84 |
85 | err = os.MkdirAll(cmdDest, os.ModePerm)
86 | if err != nil {
87 | fmt.Printf("Error opening or creating directory %s, %v", cmdDest, err)
88 | return
89 | }
90 |
91 | for _, f := range files {
92 | if strings.EqualFold(f.Name(), ".DS_Store") {
93 | continue
94 | }
95 | err := core.DealWithFile(filepath.Join(cmdSource, f.Name()), core.OutputOptions{
96 | Format: core.OutputFormat(cmdFormat),
97 | Width: cmdWidth,
98 | Height: cmdHeight,
99 | DestPath: filepath.Join(cmdDest, strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))),
100 | Interpolation: resize.InterpolationFunction(cmdResizeMode),
101 | })
102 |
103 | if err != nil {
104 | fmt.Println(err)
105 | continue
106 | }
107 | } //end of for
108 | } else {
109 | //Assume that source & destination is file
110 | //Assume that destination file does not exist, override if it's not
111 | err := core.DealWithFile(cmdSource, core.OutputOptions{
112 | Format: core.OutputFormat(cmdFormat),
113 | Width: cmdWidth,
114 | Height: cmdHeight,
115 | DestPath: cmdDest,
116 | Interpolation: resize.InterpolationFunction(cmdResizeMode),
117 | })
118 |
119 | if err != nil {
120 | fmt.Sprintln(err)
121 | return
122 | }
123 | }
124 |
125 | fmt.Println("done.")
126 | } //end of main
127 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | 木兰宽松许可证, 第2版
2 |
3 | 木兰宽松许可证, 第2版
4 | 2020年1月 http://license.coscl.org.cn/MulanPSL2
5 |
6 |
7 | 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束:
8 |
9 | 0. 定义
10 |
11 | “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
12 |
13 | “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
14 |
15 | “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
16 |
17 | “法人实体”是指提交贡献的机构及其“关联实体”。
18 |
19 | “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
20 |
21 | 1. 授予版权许可
22 |
23 | 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
24 |
25 | 2. 授予专利许可
26 |
27 | 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
28 |
29 | 3. 无商标许可
30 |
31 | “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。
32 |
33 | 4. 分发限制
34 |
35 | 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
36 |
37 | 5. 免责声明与责任限制
38 |
39 | “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
40 |
41 | 6. 语言
42 | “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。
43 |
44 | 条款结束
45 |
46 | 如何将木兰宽松许可证,第2版,应用到您的软件
47 |
48 | 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
49 |
50 | 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
51 |
52 | 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
53 |
54 | 3, 请将如下声明文本放入每个源文件的头部注释中。
55 |
56 | Copyright (c) [2022] [巴拉迪维]
57 | [ImgResizer] is licensed under Mulan PSL v2.
58 | You can use this software according to the terms and conditions of the Mulan PSL v2.
59 | You may obtain a copy of Mulan PSL v2 at:
60 | http://license.coscl.org.cn/MulanPSL2
61 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
62 | See the Mulan PSL v2 for more details.
63 |
64 |
65 | Mulan Permissive Software License,Version 2
66 |
67 | Mulan Permissive Software License,Version 2 (Mulan PSL v2)
68 | January 2020 http://license.coscl.org.cn/MulanPSL2
69 |
70 | Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions:
71 |
72 | 0. Definition
73 |
74 | Software means the program and related documents which are licensed under this License and comprise all Contribution(s).
75 |
76 | Contribution means the copyrightable work licensed by a particular Contributor under this License.
77 |
78 | Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
79 |
80 | Legal Entity means the entity making a Contribution and all its Affiliates.
81 |
82 | Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
83 |
84 | 1. Grant of Copyright License
85 |
86 | Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
87 |
88 | 2. Grant of Patent License
89 |
90 | Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
91 |
92 | 3. No Trademark License
93 |
94 | No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4.
95 |
96 | 4. Distribution Restriction
97 |
98 | You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
99 |
100 | 5. Disclaimer of Warranty and Limitation of Liability
101 |
102 | THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
103 |
104 | 6. Language
105 |
106 | THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL.
107 |
108 | END OF THE TERMS AND CONDITIONS
109 |
110 | How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software
111 |
112 | To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps:
113 |
114 | i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
115 |
116 | ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
117 |
118 | iii Attach the statement to the appropriate annotated syntax at the beginning of each source file.
119 |
120 |
121 | Copyright (c) [2022] [巴拉迪维]
122 | [ImgResizer] is licensed under Mulan PSL v2.
123 | You can use this software according to the terms and conditions of the Mulan PSL v2.
124 | You may obtain a copy of Mulan PSL v2 at:
125 | http://license.coscl.org.cn/MulanPSL2
126 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
127 | See the Mulan PSL v2 for more details.
128 |
--------------------------------------------------------------------------------