Optimize on function.

Update README.
This commit is contained in:
n0vad3v 2020-02-26 00:17:29 +08:00
parent e7a3220773
commit 087e39d1ea
No known key found for this signature in database
GPG Key ID: 8D42A0E699E50639
2 changed files with 41 additions and 39 deletions

View File

@ -26,8 +26,8 @@ It's basically between `ExpressJS` and `Fiber`, much faster than the `http` pack
### Convenience
* `webp_server`: Clone -> `npm install` -> run with `pm2`
* `webp-server(go)`: Download -> Run
* `webp_server`: Clone the repo -> `npm install` -> run with `pm2`
* `webp-server(go)`: Download a single binary -> Run
## 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
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%.
2. Run the binary like this: `./webp-server --config /path/to/config.json`, use `screen` or `tmux` to hold it currently.
3. Let Nginx to `proxy_pass http://localhost:3333/;`
1. Download the `webp-server` from [release](https://github.com/n0vad3v/webp_server_go/releases) page.
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. 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
- [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.
- [ ] 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
go get github.com/gofiber/fiber
go get github.com/chai2010/webp
go build webp_server.go
go build webp-server.go
```
**Due to the limitations of webp module, you can't cross compile this tool.**

View File

@ -30,35 +30,6 @@ type Config struct {
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() {
// Config Here
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
}
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")
}