go-watch-webp/module/image/image.go

41 lines
593 B
Go

package image
import (
"fmt"
"io"
"log"
"os"
"path"
"time"
"image/png"
"git.trj.tw/golang/go-watch-webp/module/config"
"golang.org/x/image/webp"
)
// ConvertToPng -
func ConvertToPng(file io.Reader) error {
log.Println("convert func ")
conf := config.GetConfig()
webpImg, err := webp.Decode(file)
if err != nil {
return err
}
filename := fmt.Sprintf("%d.png", time.Now().UnixNano())
fp := path.Join(conf.Dist, filename)
target, err := os.Create(fp)
if err != nil {
return err
}
if err := png.Encode(target, webpImg); err != nil {
return err
}
return nil
}