Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support theme and image directory #12

Merged
merged 7 commits into from
Sep 17, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support changing a directory of images
  • Loading branch information
tobynet committed Sep 14, 2019
commit 74c6f71de60054a84da8508085e0bfb1305cc40f
60 changes: 55 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/disintegration/imaging"
Expand All @@ -23,6 +24,13 @@ import (
"github.com/rakyll/statik/fs"
)

// Theme of longcat
type Theme struct {
Head image.Image
Body image.Image
Tail image.Image
}

func loadImage(fs http.FileSystem, n string) (image.Image, error) {
f, err := fs.Open(n)
if err != nil {
Expand All @@ -32,6 +40,47 @@ func loadImage(fs http.FileSystem, n string) (image.Image, error) {
return png.Decode(f)
}

func loadImageGlob(dir string, glob string) (image.Image, error) {
pattern := filepath.Join(dir, glob)
matches, err := filepath.Glob(pattern)
if err != nil {
log.Fatal(err)
}
if len(matches) == 0 {
log.Fatal(os.ErrNotExist, ": ", pattern)
}

data, err := ioutil.ReadFile(matches[0])
if err != nil {
log.Fatal(err)
}

img, err := png.Decode(bytes.NewReader(data))
if err != nil {
log.Fatal(err)
}
return img, nil
}

func loadTheme(dir string) (Theme, error) {
theme := Theme{}
if dir == "" {
fs, err := fs.New()
if err != nil {
return theme, err
}

theme.Head, _ = loadImage(fs, "/data01.png")
theme.Body, _ = loadImage(fs, "/data02.png")
theme.Tail, _ = loadImage(fs, "/data03.png")
} else {
theme.Head, _ = loadImageGlob(dir, "*1.png")
theme.Body, _ = loadImageGlob(dir, "*2.png")
theme.Tail, _ = loadImageGlob(dir, "*3.png")
}
return theme, nil
}
mattn marked this conversation as resolved.
Show resolved Hide resolved

func saveImage(filename string, img image.Image) error {
var buf bytes.Buffer
err := png.Encode(&buf, img)
Expand All @@ -49,6 +98,7 @@ func main() {
var flipV bool
var isHorizontal bool
var filename string
var imageDir string

flag.IntVar(&nlong, "n", 1, "how long cat")
flag.IntVar(&ncolumns, "l", 1, "number of columns")
Expand All @@ -57,16 +107,16 @@ func main() {
flag.BoolVar(&flipV, "R", false, "flip vertical")
flag.BoolVar(&isHorizontal, "H", false, "holizontal-mode")
flag.StringVar(&filename, "o", "", "output image file")
flag.StringVar(&imageDir, "d", "", "directory of images(dir/*{1,2,3}.png)")
flag.Parse()

fs, err := fs.New()
theme, err := loadTheme(imageDir)
if err != nil {
log.Fatal(err)
}

img1, _ := loadImage(fs, "/data01.png")
img2, _ := loadImage(fs, "/data02.png")
img3, _ := loadImage(fs, "/data03.png")
img1 := theme.Head
img2 := theme.Body
img3 := theme.Tail

if flipH {
img1 = imaging.FlipH(img1)
Expand Down