diff --git a/admin/sync/themes.go b/admin/sync/themes.go index f3b9fb7f..a9b5366e 100644 --- a/admin/sync/themes.go +++ b/admin/sync/themes.go @@ -3,13 +3,17 @@ package sync import ( "errors" "fmt" + "image" + "image/jpeg" "math/rand" + "os" "path" "regexp" "strings" "unicode" "github.com/julienschmidt/httprouter" + "golang.org/x/image/draw" "gopkg.in/russross/blackfriday.v2" "srs.epita.fr/fic-server/libfic" ) @@ -31,6 +35,42 @@ func GetThemes(i Importer) ([]string, error) { return themes, nil } +// resizePicture makes the given image just fill the given rectangle. +func resizePicture(importedPath string, rect image.Rectangle) error { + if fl, err := os.Open(importedPath); err != nil { + return err + } else { + if src, _, err := image.Decode(fl); err != nil { + fl.Close() + return err + } else if src.Bounds().Max.X > rect.Max.X && src.Bounds().Max.Y > rect.Max.Y { + fl.Close() + + mWidth := rect.Max.Y * src.Bounds().Max.X / src.Bounds().Max.Y + mHeight := rect.Max.X * src.Bounds().Max.Y / src.Bounds().Max.X + + if mWidth > rect.Max.X { + rect.Max.X = mWidth + } else { + rect.Max.Y = mHeight + } + dst := image.NewRGBA(rect) + draw.CatmullRom.Scale(dst, rect, src, src.Bounds(), draw.Over, nil) + + dstFile, err := os.Create(importedPath) + if err != nil { + return err + } + defer dstFile.Close() + + if err = jpeg.Encode(dstFile, dst, &jpeg.Options{100}); err != nil { + return err + } + } + } + return nil +} + // getAuthors parses the AUTHORS file. func getAuthors(i Importer, tname string) ([]string, error) { if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil { @@ -126,6 +166,10 @@ func SyncThemes(i Importer) []string { if len(btheme.Image) > 0 { if _, err := i.importFile(btheme.Image, func(filePath string, origin string) (interface{}, error) { + if err := resizePicture(filePath, image.Rect(0, 0, 500, 300)); err != nil { + return nil, err + } + btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir) return nil, nil }); err != nil {