mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
webp encoder
This commit is contained in:
parent
6b5f1e6e92
commit
b813dcfc77
8
go.mod
Normal file
8
go.mod
Normal file
@ -0,0 +1,8 @@
|
||||
module webp_server_go
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/chai2010/webp v1.1.0
|
||||
github.com/gofiber/fiber v1.4.0
|
||||
)
|
BIN
pics/webp_server.jpg
Normal file
BIN
pics/webp_server.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 206 KiB |
@ -2,13 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gofiber/fiber"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"path"
|
||||
"github.com/chai2010/webp"
|
||||
"github.com/gofiber/fiber"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@ -17,7 +23,26 @@ type Config struct {
|
||||
IMG_PATH string
|
||||
QUALITY string
|
||||
ALLOWED_TYPES []string
|
||||
}
|
||||
|
||||
func webpEncoder(p1, p2 string, quality float32) {
|
||||
var buf bytes.Buffer
|
||||
var img image.Image
|
||||
data, _ := ioutil.ReadFile(p1)
|
||||
if strings.Contains(p1, "jpg") || strings.Contains(p1, "jpeg") {
|
||||
img, _ = jpeg.Decode(bytes.NewReader(data))
|
||||
} else if strings.Contains(p1, "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 main() {
|
||||
@ -49,7 +74,7 @@ func main() {
|
||||
IMG_PATH := c.Path()
|
||||
|
||||
// jpg
|
||||
IMG_EXT := strings.Split(path.Ext(IMG_PATH),".")[1]
|
||||
IMG_EXT := strings.Split(path.Ext(IMG_PATH), ".")[1]
|
||||
|
||||
// tsuki.jpg
|
||||
IMG_NAME := path.Base(IMG_PATH)
|
||||
@ -93,22 +118,14 @@ func main() {
|
||||
|
||||
if imageExists(WEBP_ABSOLUTE_PATH) {
|
||||
c.SendFile(WEBP_ABSOLUTE_PATH)
|
||||
} else{
|
||||
} else {
|
||||
// Mkdir
|
||||
os.MkdirAll(DIR_ABSOLUTE_PATH , os.ModePerm)
|
||||
os.MkdirAll(DIR_ABSOLUTE_PATH, os.ModePerm)
|
||||
|
||||
// cwebp -q 60 Cute-Baby-Girl.png -o Cute-Baby-Girl.webp
|
||||
OS_CMD := exec.Command("./webp/cwebp","-q",QUALITY,IMG_ABSOLUTE_PATH,"-o",WEBP_ABSOLUTE_PATH)
|
||||
var out bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
OS_CMD.Stdout = &out
|
||||
OS_CMD.Stderr = &stderr
|
||||
err := OS_CMD.Run()
|
||||
if err != nil {
|
||||
fmt.Println(stderr.String())
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
q, _ := strconv.ParseFloat(QUALITY, 32)
|
||||
webpEncoder(IMG_ABSOLUTE_PATH, WEBP_ABSOLUTE_PATH, float32(q))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
@ -121,7 +138,7 @@ func main() {
|
||||
|
||||
func load_config(path string) Config {
|
||||
var config Config
|
||||
json_object,err := os.Open(path)
|
||||
json_object, err := os.Open(path)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user