mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Optimize on function.
Update README.
This commit is contained in:
parent
e7a3220773
commit
087e39d1ea
22
README.md
22
README.md
@ -26,8 +26,8 @@ It's basically between `ExpressJS` and `Fiber`, much faster than the `http` pack
|
|||||||
|
|
||||||
### Convenience
|
### Convenience
|
||||||
|
|
||||||
* `webp_server`: Clone -> `npm install` -> run with `pm2`
|
* `webp_server`: Clone the repo -> `npm install` -> run with `pm2`
|
||||||
* `webp-server(go)`: Download -> Run
|
* `webp-server(go)`: Download a single binary -> Run
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@ -35,19 +35,21 @@ Regarding the `IMG_PATH` section in `config.json`.
|
|||||||
If you are serving images at `https://example.com/pics/tsuki.jpg` and
|
If you are serving images at `https://example.com/pics/tsuki.jpg` and
|
||||||
your files are at `/var/www/image/pics/tsuki.jpg`, then `IMG_PATH` shall be `/var/www/image`.
|
your files are at `/var/www/image/pics/tsuki.jpg`, then `IMG_PATH` shall be `/var/www/image`.
|
||||||
|
|
||||||
1. Edit the `config.json` to face your need, default convert quality is 80%.
|
1. Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page.
|
||||||
2. Run the binary like this: `./webp-server --config /path/to/config.json`, use `screen` or `tmux` to hold it currently.
|
2. Create a `config.json` like [this](https://github.com/n0vad3v/webp_server_go/blob/master/config.json) to face your need, default convert quality is 80%.
|
||||||
3. Let Nginx to `proxy_pass http://localhost:3333/;`
|
3. Run the binary like this: `./webp-server --config /path/to/config.json`, use `screen` or `tmux` to hold it currently.(Will add systemd later)
|
||||||
|
4. Let Nginx to `proxy_pass http://localhost:3333/;`
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
- [x] This version doesn't support header-based-output, which means Safari users will not see the converted `webp` images, this should be fixed in later releases.
|
- [x] This version doesn't support header-based-output, which means Safari users will not see the converted `webp` images, this should be fixed in later releases.
|
||||||
- [ ] Multi platform support.
|
- [ ] Multi platform support.
|
||||||
|
- [ ] A better way to supervise the program.
|
||||||
|
- [ ] Get rid of render-blocking effect on first render.
|
||||||
|
|
||||||
## build your own binary
|
|
||||||
Install golang, enable go module, and then...
|
## Build
|
||||||
|
Install golang, enable go module, clone the repo, and then...
|
||||||
```shell script
|
```shell script
|
||||||
go get github.com/gofiber/fiber
|
go build webp-server.go
|
||||||
go get github.com/chai2010/webp
|
|
||||||
go build webp_server.go
|
|
||||||
```
|
```
|
||||||
**Due to the limitations of webp module, you can't cross compile this tool.**
|
**Due to the limitations of webp module, you can't cross compile this tool.**
|
@ -30,35 +30,6 @@ type Config struct {
|
|||||||
|
|
||||||
var configPath string
|
var configPath string
|
||||||
|
|
||||||
func GetFileContentType(buffer []byte) (string, error) {
|
|
||||||
// Use the net/http package's handy DectectContentType function. Always returns a valid
|
|
||||||
// content-type by returning "application/octet-stream" if no others seemed to match.
|
|
||||||
contentType := http.DetectContentType(buffer)
|
|
||||||
return contentType, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func webpEncoder(p1, p2 string, quality float32) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
var img image.Image
|
|
||||||
|
|
||||||
data, _ := ioutil.ReadFile(p1)
|
|
||||||
contentType, _ := GetFileContentType(data[:512])
|
|
||||||
if strings.Contains(contentType, "jpeg") {
|
|
||||||
img, _ = jpeg.Decode(bytes.NewReader(data))
|
|
||||||
} else if strings.Contains(contentType, "png") {
|
|
||||||
img, _ = png.Decode(bytes.NewReader(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := webp.Encode(&buf, img, &webp.Options{Lossless: true, Quality: quality}); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
if err := ioutil.WriteFile(p2, buf.Bytes(), 0666); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Save output.webp ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Config Here
|
// Config Here
|
||||||
flag.StringVar(&configPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)")
|
flag.StringVar(&configPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)")
|
||||||
@ -198,3 +169,32 @@ func Find(slice []string, val string) (int, bool) {
|
|||||||
}
|
}
|
||||||
return -1, false
|
return -1, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetFileContentType(buffer []byte) string {
|
||||||
|
// Use the net/http package's handy DectectContentType function. Always returns a valid
|
||||||
|
// content-type by returning "application/octet-stream" if no others seemed to match.
|
||||||
|
contentType := http.DetectContentType(buffer)
|
||||||
|
return contentType
|
||||||
|
}
|
||||||
|
|
||||||
|
func webpEncoder(p1, p2 string, quality float32) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var img image.Image
|
||||||
|
|
||||||
|
data, _ := ioutil.ReadFile(p1)
|
||||||
|
contentType := GetFileContentType(data[:512])
|
||||||
|
if strings.Contains(contentType, "jpeg") {
|
||||||
|
img, _ = jpeg.Decode(bytes.NewReader(data))
|
||||||
|
} else if strings.Contains(contentType, "png") {
|
||||||
|
img, _ = png.Decode(bytes.NewReader(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := webp.Encode(&buf, img, &webp.Options{Lossless: true, Quality: quality}); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(p2, buf.Bytes(), 0666); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Save to webp ok")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user