sync: resizePicture uses image from importer instead of local file
This commit is contained in:
parent
c7d1d7ce4c
commit
b55151623c
2 changed files with 13 additions and 8 deletions
|
@ -398,7 +398,7 @@ func SyncExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*f
|
||||||
if len(e.Image) > 0 {
|
if len(e.Image) > 0 {
|
||||||
if _, err := i.importFile(e.Image,
|
if _, err := i.importFile(e.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 {
|
if err := resizePicture(i, origin, filePath, image.Rect(0, 0, 500, 300)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -40,15 +41,19 @@ func GetThemes(i Importer) (themes []string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// resizePicture makes the given image just fill the given rectangle.
|
// resizePicture makes the given image just fill the given rectangle.
|
||||||
func resizePicture(importedPath string, rect image.Rectangle) error {
|
func resizePicture(i Importer, imgPath string, importedPath string, rect image.Rectangle) error {
|
||||||
if fl, err := os.Open(importedPath); err != nil {
|
if fl, err := i.GetFile(imgPath); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
if src, _, err := image.Decode(fl); err != nil {
|
if src, _, err := image.Decode(fl); err != nil {
|
||||||
fl.Close()
|
if flc, ok := fl.(io.ReadCloser); ok {
|
||||||
|
flc.Close()
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
} else if src.Bounds().Max.X > rect.Max.X && src.Bounds().Max.Y > rect.Max.Y {
|
} else if src.Bounds().Max.X > rect.Max.X && src.Bounds().Max.Y > rect.Max.Y {
|
||||||
fl.Close()
|
if flc, ok := fl.(io.ReadCloser); ok {
|
||||||
|
flc.Close()
|
||||||
|
}
|
||||||
|
|
||||||
mWidth := rect.Max.Y * src.Bounds().Max.X / src.Bounds().Max.Y
|
mWidth := rect.Max.Y * src.Bounds().Max.X / src.Bounds().Max.Y
|
||||||
mHeight := rect.Max.X * src.Bounds().Max.Y / src.Bounds().Max.X
|
mHeight := rect.Max.X * src.Bounds().Max.Y / src.Bounds().Max.X
|
||||||
|
@ -61,7 +66,7 @@ func resizePicture(importedPath string, rect image.Rectangle) error {
|
||||||
dst := image.NewRGBA(rect)
|
dst := image.NewRGBA(rect)
|
||||||
draw.CatmullRom.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
|
draw.CatmullRom.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
|
||||||
|
|
||||||
dstFile, err := os.Create(strings.TrimSuffix(importedPath, ".jpg") + ".thumb.jpg")
|
dstFile, err := fileWriter(strings.TrimSuffix(importedPath, ".jpg") + ".thumb.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -71,7 +76,7 @@ func resizePicture(importedPath string, rect image.Rectangle) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dstFile, err := os.Create(strings.TrimSuffix(importedPath, ".jpg") + ".thumb.jpg")
|
dstFile, err := fileWriter(strings.TrimSuffix(importedPath, ".jpg") + ".thumb.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -278,7 +283,7 @@ func SyncThemeFiles(i Importer, btheme *fic.Theme) (errs error) {
|
||||||
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(origin, filePath, image.Rect(0, 0, 500, 300)); err != nil {
|
if err := resizePicture(i, origin, filePath, image.Rect(0, 0, 500, 300)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue