You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.4 KiB

package main
import (
"bufio"
"encoding/json"
"image"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"path/filepath"
)
// FileEntry contains informations for PowerSlide.
type FileEntry struct {
Name string `json:"src"`
Width int `json:"w"`
Height int `json:"h"`
Caption string `json:"title"`
}
func main() {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
entries := buildEntries(dir)
encodedJSON, err := json.MarshalIndent(entries, "", " ")
if err != nil {
panic(err)
}
outputFile, err := os.Create(filepath.Join(dir, "data.json"))
if err != nil {
panic(err)
}
defer outputFile.Close()
outputWriter := bufio.NewWriter(outputFile)
_, err = outputWriter.Write(encodedJSON)
if err != nil {
panic(err)
}
outputWriter.Flush()
}
func buildEntries(root string) []FileEntry {
var entries []FileEntry
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
ext := filepath.Ext(path)
if !(ext == ".png" || ext == ".jpg" || ext == ".gif") {
return nil
}
width, height := getImageDimensions(path)
entries = append(entries, FileEntry{info.Name(), width, height, ""})
return nil
})
return entries
}
func getImageDimensions(path string) (int, int) {
file, err := os.Open(path)
if err != nil {
log.Println(err)
return 0, 0
}
defer file.Close()
config, _, err := image.DecodeConfig(file)
if err != nil {
log.Println(err)
return 0, 0
}
return config.Width, config.Height
}