Skip to content

Commit

Permalink
v1.2 版本内容
Browse files Browse the repository at this point in the history
  • Loading branch information
barats committed Jun 19, 2022
1 parent 5b0de2e commit f0ac5fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
批量图片等比缩放、类型转换工具
1. 支持图片类型:bmp、tiff、jpg、jpeg、gif、png、webp
1. 支持类型转换为:bmp、tiff、jpg、jpeg、gif、png
1. 支持自定义宽度、高度
1. 支持自定义宽度、高度
1. 五种等比缩放模式

## 使用方法

```
ImgResizer -source {source} -dest {dest} -mode {mode}
-dest string
Destination file or directory
Destination file or directory.
-format string
Ouput format (png|jpg|jpeg|bmp|tiff|gif)
Ouput format. Supported values: png|jpg|jpeg|bmp|tiff|gif. Omit to keep original format.
-height int
Destination height (default 128)
Destination height. Omit to keep original height (default -1)
-help
Show help message
Show help message.
-mode int
0 - (Default) Nearest-neighbor interpolation
1 - Bilinear interpolation
Expand All @@ -27,25 +26,34 @@ ImgResizer -source {source} -dest {dest} -mode {mode}
4 - Lanczos resampling with a=2
5 - Lanczos resampling with a=3
-source string
Source file or directory
Source file or directory.
-width int
Destination width (default 300)
Destination width. Omit to keep original width (default -1)
```

## 批量等比缩放处理
## 注意事项

1. **如果不需要改变原图类型,请省略 `-format` 参数**
1. **webp 格式图片,默认转换为 png 格式处理(目前没有 webp 图片的高效、简洁处理办法)**
1. **如果不需要改变原图尺寸,请同时省略 `-width``-height` 参数**

## 使用示例

### 1. 批量等比缩放

```
ImgResizer -source ~/Desktop/pics -dest ~/Desktop/new_pics -mode 5
ImgResizer -source ~/pics -dest ~/new_pics -mode 5 -height 128 -width 300
```

## 单个文件指定宽度缩放
### 2. 单文件指定宽度

```
ImgResizer -source ~/pics/hello.gif -dest ~/newpics/wow.gif -width 900
```

## 批量文件类型转换
### 3. 批量类型转换

```
ImgResizer -source ~/Desktop/pics -dest ~/Desktop/new_pics -format jpg
ImgResizer -source ~/pics -dest ~/new_pics -format jpg
```

18 changes: 13 additions & 5 deletions core/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func DealWithFile(source string, option OutputOptions) error {
}
defer file.Close()

fileFormat, err := guessImageType(file)
fileFormat, width, height, err := retrieveImageInfo(file)
if err != nil {
fmt.Printf("Could not guess mime type of file %s, %v", source, err)
return err
Expand Down Expand Up @@ -72,6 +72,14 @@ func DealWithFile(source string, option OutputOptions) error {
return err
}

if option.Width == -1 {
option.Width = width
}

if option.Height == -1 {
option.Height = height
}

afterResize := resize.Resize(uint(option.Width), uint(option.Height), data, option.Interpolation)

if strings.EqualFold("", string(option.Format)) {
Expand Down Expand Up @@ -101,11 +109,11 @@ func DealWithFile(source string, option OutputOptions) error {

//
//Guess image type
func guessImageType(r io.Reader) (string, error) {
_, format, err := image.DecodeConfig(r)
func retrieveImageInfo(r io.Reader) (string, int, int, error) {
config, format, err := image.DecodeConfig(r)
if err != nil {
return "", err
return "", -1, -1, err
}

return format, nil
return format, config.Width, config.Height, nil
}
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/nfnt/resize"
)

const Version string = "v1.1"
const Version string = "v1.2"

var (
cmdSource string
Expand All @@ -25,12 +25,12 @@ var (
)

func init() {
flag.StringVar(&cmdFormat, "format", "", "Ouput format (png|jpg|jpeg|bmp|tiff|gif)")
flag.BoolVar(&cmdHelp, "help", false, "Show help message")
flag.IntVar(&cmdWidth, "width", 300, "Destination width")
flag.IntVar(&cmdHeight, "height", 128, "Destination height")
flag.StringVar(&cmdSource, "source", "", "Source file or directory")
flag.StringVar(&cmdDest, "dest", "", "Destination file or directory")
flag.StringVar(&cmdFormat, "format", "", "Ouput format. Supported values: png|jpg|jpeg|bmp|tiff|gif. Omit to keep original format.")
flag.BoolVar(&cmdHelp, "help", false, "Show help message.")
flag.IntVar(&cmdWidth, "width", -1, "Destination width. Omit to keep original width")
flag.IntVar(&cmdHeight, "height", -1, "Destination height. Omit to keep original height")
flag.StringVar(&cmdSource, "source", "", "Source file or directory.")
flag.StringVar(&cmdDest, "dest", "", "Destination file or directory.")
flag.IntVar(&cmdResizeMode, "mode", 0, `0 - (Default) Nearest-neighbor interpolation
1 - Bilinear interpolation
2 - Bicubic interpolation
Expand All @@ -39,7 +39,7 @@ func init() {
5 - Lanczos resampling with a=3`)

flag.Usage = func() {
fmt.Printf("Usage of ImgResizer %s \nImgResizer -source {source} -dest {dest} -mode {mode}\n\n", Version)
fmt.Printf("Usage of ImgResizer %s \nFor more information, please visit: \nhttps://github.com/barats/ImgResizer \nhttps://gitee.com/barat/imgresizer \n\nImgResizer -source {source} -dest {dest} -mode {mode}\n", Version)
flag.PrintDefaults()
}
}
Expand All @@ -53,6 +53,11 @@ func main() {
return
}

if strings.EqualFold("", strings.TrimSpace(cmdSource)) || strings.EqualFold("", strings.TrimSpace(cmdDest)) {
fmt.Println("Missing parameter <-source> or <-dest>. Please -h or -help to show help message.")
return
}

sourceInfo, err := os.Stat(cmdSource)
if err != nil {
fmt.Printf("Cant not open %s, error %v", cmdSource, err)
Expand Down

0 comments on commit f0ac5fc

Please sign in to comment.