Support configuring image size
This commit is contained in:
parent
0ccaef69d7
commit
45cda28e42
@ -14,6 +14,8 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/writeas/text-pic"
|
"github.com/writeas/text-pic"
|
||||||
@ -25,6 +27,7 @@ var (
|
|||||||
font = flag.String("font", "serif", "Post font (options: \"serif\", \"sans\", \"mono\")")
|
font = flag.String("font", "serif", "Post font (options: \"serif\", \"sans\", \"mono\")")
|
||||||
instance = flag.String("i", "write.as", "WriteFreely instance hostname (e.g. pencil.writefree.ly)")
|
instance = flag.String("i", "write.as", "WriteFreely instance hostname (e.g. pencil.writefree.ly)")
|
||||||
author = flag.String("u", "", "WriteFreely author username (for multi-user instances)")
|
author = flag.String("u", "", "WriteFreely author username (for multi-user instances)")
|
||||||
|
size = flag.String("size", "1024", "Image size, either a single number for a square (e.g. \"900\") or a combined width and height (e.g. \"1080x1920\")")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -36,6 +39,34 @@ func main() {
|
|||||||
log.Info("Invalid font given. Options: \"serif\", \"sans\", \"mono\"")
|
log.Info("Invalid font given. Options: \"serif\", \"sans\", \"mono\"")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
// Parse image dimensions and validate
|
||||||
|
var w, h int
|
||||||
|
if strings.ContainsRune(*size, 'x') {
|
||||||
|
parts := strings.Split(*size, "x")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
log.Info("Invalid --size given. Must be a single number for a square (e.g. \"900\") or a combined width and height (e.g. \"1080x1920\")")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
w, err = strconv.Atoi(parts[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Unable to parse --size dimension '%s': %s", parts[0], err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
h, err = strconv.Atoi(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Unable to parse --size dimension '%s': %s", parts[1], err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sq, err := strconv.Atoi(*size)
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Unable to parse --size: %s", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
w = sq
|
||||||
|
h = sq
|
||||||
|
}
|
||||||
|
|
||||||
log.Info("Reading input...")
|
log.Info("Reading input...")
|
||||||
in, err := ioutil.ReadAll(os.Stdin)
|
in, err := ioutil.ReadAll(os.Stdin)
|
||||||
@ -46,7 +77,7 @@ func main() {
|
|||||||
|
|
||||||
log.Info("Generating image...")
|
log.Info("Generating image...")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), *outputFile)
|
err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), w, h, *outputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("%s", err)
|
log.Error("%s", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -33,9 +33,7 @@ func loadFont(dc *gg.Context, fontFace string, bold bool, points float64) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateImage(opt *ContentOptions, outputFilename string) error {
|
func GenerateImage(opt *ContentOptions, w, h int, outputFilename string) error {
|
||||||
w := 900
|
|
||||||
h := 900
|
|
||||||
wf := float64(w)
|
wf := float64(w)
|
||||||
hf := float64(h)
|
hf := float64(h)
|
||||||
dc := gg.NewContext(w, h)
|
dc := gg.NewContext(w, h)
|
||||||
|
Loading…
Reference in New Issue
Block a user