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 _, err := i.importFile(e.Image,
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -40,15 +41,19 @@ func GetThemes(i Importer) (themes []string, err error) {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
func resizePicture(i Importer, imgPath string, importedPath string, rect image.Rectangle) error {
|
||||
if fl, err := i.GetFile(imgPath); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if src, _, err := image.Decode(fl); err != nil {
|
||||
fl.Close()
|
||||
if flc, ok := fl.(io.ReadCloser); ok {
|
||||
flc.Close()
|
||||
}
|
||||
return err
|
||||
} 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
|
||||
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)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -71,7 +76,7 @@ func resizePicture(importedPath string, rect image.Rectangle) error {
|
|||
return err
|
||||
}
|
||||
} else {
|
||||
dstFile, err := os.Create(strings.TrimSuffix(importedPath, ".jpg") + ".thumb.jpg")
|
||||
dstFile, err := fileWriter(strings.TrimSuffix(importedPath, ".jpg") + ".thumb.jpg")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -278,7 +283,7 @@ func SyncThemeFiles(i Importer, btheme *fic.Theme) (errs error) {
|
|||
if len(btheme.Image) > 0 {
|
||||
if _, err := i.importFile(btheme.Image,
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue