[#87] added additional thumb resizers
This commit is contained in:
@@ -3,10 +3,12 @@ package filesystem
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -230,18 +232,31 @@ func (s *System) Serve(response http.ResponseWriter, fileKey string, name string
|
||||
return err
|
||||
}
|
||||
|
||||
var ThumbSizeRegex = regexp.MustCompile(`^(\d+)x(\d+)(t|b|f)?$`)
|
||||
|
||||
// CreateThumb creates a new thumb image for the file at originalKey location.
|
||||
// The new thumb file is stored at thumbKey location.
|
||||
//
|
||||
// thumbSize is in the format "WxH", eg. "100x50".
|
||||
func (s *System) CreateThumb(originalKey string, thumbKey, thumbSize string, cropCenter bool) error {
|
||||
thumbSizeParts := strings.SplitN(thumbSize, "x", 2)
|
||||
if len(thumbSizeParts) != 2 {
|
||||
return errors.New("Thumb size must be in WxH format.")
|
||||
// thumbSize is in the format:
|
||||
// - 0xH (eg. 0x100) - resize to H height preserving the aspect ratio
|
||||
// - Wx0 (eg. 300x0) - resize to W width preserving the aspect ratio
|
||||
// - WxH (eg. 300x100) - resize and crop to WxH viewbox (from center)
|
||||
// - WxHt (eg. 300x100t) - resize and crop to WxH viewbox (from top)
|
||||
// - WxHb (eg. 300x100b) - resize and crop to WxH viewbox (from bottom)
|
||||
// - WxHf (eg. 300x100f) - fit inside a WxH viewbox (without cropping)
|
||||
func (s *System) CreateThumb(originalKey string, thumbKey, thumbSize string) error {
|
||||
sizeParts := ThumbSizeRegex.FindStringSubmatch(thumbSize)
|
||||
if len(sizeParts) != 4 {
|
||||
return errors.New("Thumb size must be in WxH, WxHt, WxHb or WxHf format.")
|
||||
}
|
||||
|
||||
width, _ := strconv.Atoi(thumbSizeParts[0])
|
||||
height, _ := strconv.Atoi(thumbSizeParts[1])
|
||||
width, _ := strconv.Atoi(sizeParts[1])
|
||||
height, _ := strconv.Atoi(sizeParts[2])
|
||||
resizeType := sizeParts[3]
|
||||
|
||||
if width == 0 && height == 0 {
|
||||
return errors.New("Thumb width and height cannot be zero at the same time.")
|
||||
}
|
||||
|
||||
// fetch the original
|
||||
r, readErr := s.bucket.NewReader(s.ctx, originalKey, nil)
|
||||
@@ -256,14 +271,27 @@ func (s *System) CreateThumb(originalKey string, thumbKey, thumbSize string, cro
|
||||
return decodeErr
|
||||
}
|
||||
|
||||
// determine crop anchor
|
||||
cropAnchor := imaging.Center
|
||||
if !cropCenter {
|
||||
cropAnchor = imaging.Top
|
||||
}
|
||||
var thumbImg *image.NRGBA
|
||||
|
||||
// create thumb imaging object
|
||||
thumbImg := imaging.Fill(img, width, height, cropAnchor, imaging.CatmullRom)
|
||||
if width == 0 || height == 0 {
|
||||
// force resize preserving aspect ratio
|
||||
thumbImg = imaging.Resize(img, width, height, imaging.CatmullRom)
|
||||
} else {
|
||||
switch resizeType {
|
||||
case "f":
|
||||
// fit
|
||||
thumbImg = imaging.Fit(img, width, height, imaging.CatmullRom)
|
||||
case "t":
|
||||
// fill and crop from top
|
||||
thumbImg = imaging.Fill(img, width, height, imaging.Top, imaging.CatmullRom)
|
||||
case "b":
|
||||
// fill and crop from bottom
|
||||
thumbImg = imaging.Fill(img, width, height, imaging.Bottom, imaging.CatmullRom)
|
||||
default:
|
||||
// fill and crop from center
|
||||
thumbImg = imaging.Fill(img, width, height, imaging.Center, imaging.CatmullRom)
|
||||
}
|
||||
}
|
||||
|
||||
// open a thumb storage writer (aka. prepare for upload)
|
||||
w, writerErr := s.bucket.NewWriter(s.ctx, thumbKey, nil)
|
||||
@@ -274,7 +302,6 @@ func (s *System) CreateThumb(originalKey string, thumbKey, thumbSize string, cro
|
||||
// thumb encode (aka. upload)
|
||||
if err := imaging.Encode(w, thumbImg, imaging.PNG); err != nil {
|
||||
w.Close()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user