forked from jason/cpolis
Automatically resize images and save them as webp
This commit is contained in:
@ -12,36 +12,40 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ArticleDir string
|
||||
ConfigFile string
|
||||
DBName string
|
||||
Description string
|
||||
Domain string
|
||||
FirebaseKey string
|
||||
KeyFile string
|
||||
Link string
|
||||
LogFile string
|
||||
PDFDir string
|
||||
PicsDir string
|
||||
Port string
|
||||
RSSFile string
|
||||
Title string
|
||||
WebDir string
|
||||
ArticleDir string
|
||||
ConfigFile string
|
||||
DBName string
|
||||
Description string
|
||||
Domain string
|
||||
FirebaseKey string
|
||||
KeyFile string
|
||||
Link string
|
||||
LogFile string
|
||||
PDFDir string
|
||||
PicsDir string
|
||||
Port string
|
||||
RSSFile string
|
||||
Title string
|
||||
WebDir string
|
||||
MaxImgHeight int
|
||||
MaxImgWidth int
|
||||
}
|
||||
|
||||
func newConfig() *Config {
|
||||
return &Config{
|
||||
ArticleDir: "/var/www/cpolis/articles",
|
||||
ConfigFile: "/etc/cpolis/config.toml",
|
||||
DBName: "cpolis",
|
||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||
KeyFile: "/var/www/cpolis/cpolis.key",
|
||||
LogFile: "/var/log/cpolis.log",
|
||||
PDFDir: "/var/www/cpolis/pdfs",
|
||||
PicsDir: "/var/www/cpolis/pics",
|
||||
Port: ":8080",
|
||||
RSSFile: "/var/www/cpolis/cpolis.rss",
|
||||
WebDir: "/var/www/cpolis/web",
|
||||
ArticleDir: "/var/www/cpolis/articles",
|
||||
ConfigFile: "/etc/cpolis/config.toml",
|
||||
DBName: "cpolis",
|
||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||
KeyFile: "/var/www/cpolis/cpolis.key",
|
||||
LogFile: "/var/log/cpolis.log",
|
||||
MaxImgHeight: 1080,
|
||||
MaxImgWidth: 1920,
|
||||
PDFDir: "/var/www/cpolis/pdfs",
|
||||
PicsDir: "/var/www/cpolis/pics",
|
||||
Port: ":8080",
|
||||
RSSFile: "/var/www/cpolis/cpolis.rss",
|
||||
WebDir: "/var/www/cpolis/web",
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,6 +114,8 @@ func (c *Config) handleCliArgs() error {
|
||||
flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file")
|
||||
flag.StringVar(&c.Title, "title", c.Title, "channel title")
|
||||
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
||||
flag.IntVar(&c.MaxImgHeight, "height", c.MaxImgHeight, "maximum image height")
|
||||
flag.IntVar(&c.MaxImgWidth, "width", c.MaxImgWidth, "maximum image width")
|
||||
flag.IntVar(&port, "port", port, "port")
|
||||
flag.Parse()
|
||||
|
||||
@ -186,6 +192,14 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
return fmt.Errorf("error setting up file: %v", err)
|
||||
}
|
||||
|
||||
if cliConfig.MaxImgHeight != defaultConfig.MaxImgHeight {
|
||||
c.MaxImgHeight = cliConfig.MaxImgHeight
|
||||
}
|
||||
|
||||
if cliConfig.MaxImgWidth != defaultConfig.MaxImgWidth {
|
||||
c.MaxImgWidth = cliConfig.MaxImgWidth
|
||||
}
|
||||
|
||||
if cliConfig.PDFDir != defaultConfig.PDFDir {
|
||||
c.PDFDir = cliConfig.PDFDir
|
||||
}
|
||||
|
38
cmd/backend/images.go
Normal file
38
cmd/backend/images.go
Normal file
@ -0,0 +1,38 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/chai2010/webp"
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func SaveImage(c *Config, src io.Reader) (string, error) {
|
||||
img, err := imaging.Decode(src, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error decoding image: %v", err)
|
||||
}
|
||||
|
||||
if img.Bounds().Dy() > c.MaxImgHeight {
|
||||
img = imaging.Resize(img, 0, c.MaxImgHeight, imaging.Lanczos)
|
||||
}
|
||||
if img.Bounds().Dx() > c.MaxImgWidth {
|
||||
img = imaging.Resize(img, c.MaxImgWidth, 0, imaging.Lanczos)
|
||||
}
|
||||
|
||||
filename := fmt.Sprint(c.PicsDir, "/", uuid.New(), ".webp")
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating new image file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if err = webp.Encode(file, img, &webp.Options{Lossless: true}); err != nil {
|
||||
return "", fmt.Errorf("error encoding image as webp: %v", err)
|
||||
}
|
||||
|
||||
return filename, nil
|
||||
}
|
Reference in New Issue
Block a user