sync: resize heading pictures
This commit is contained in:
parent
a8b6e9fba5
commit
8f998485bb
@ -3,13 +3,17 @@ package sync
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"golang.org/x/image/draw"
|
||||||
"gopkg.in/russross/blackfriday.v2"
|
"gopkg.in/russross/blackfriday.v2"
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
)
|
)
|
||||||
@ -31,6 +35,42 @@ func GetThemes(i Importer) ([]string, error) {
|
|||||||
return themes, nil
|
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.
|
// getAuthors parses the AUTHORS file.
|
||||||
func getAuthors(i Importer, tname string) ([]string, error) {
|
func getAuthors(i Importer, tname string) ([]string, error) {
|
||||||
if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
|
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 len(btheme.Image) > 0 {
|
||||||
if _, err := i.importFile(btheme.Image,
|
if _, err := i.importFile(btheme.Image,
|
||||||
func(filePath string, origin string) (interface{}, error) {
|
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)
|
btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user