2021-09-05 17:10:52 +02:00
|
|
|
package crop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
2021-10-06 07:10:50 +02:00
|
|
|
"os"
|
2021-09-05 21:19:52 +02:00
|
|
|
"path"
|
2021-09-05 17:10:52 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-09-05 17:10:52 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
2021-09-05 19:10:44 +02:00
|
|
|
// Filenames of usable thumb sizes.
|
|
|
|
var thumbFileNames = []string{
|
|
|
|
"%s_720x720_fit.jpg",
|
|
|
|
"%s_1280x1024_fit.jpg",
|
|
|
|
"%s_1920x1200_fit.jpg",
|
|
|
|
"%s_2048x2048_fit.jpg",
|
|
|
|
"%s_4096x4096_fit.jpg",
|
|
|
|
"%s_7680x4320_fit.jpg",
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// Suitable thumb file sizes.
|
2021-09-05 19:10:44 +02:00
|
|
|
var thumbFileSizes = []thumb.Size{
|
|
|
|
thumb.Sizes[thumb.Fit720],
|
|
|
|
thumb.Sizes[thumb.Fit1280],
|
|
|
|
thumb.Sizes[thumb.Fit1920],
|
|
|
|
thumb.Sizes[thumb.Fit2048],
|
|
|
|
thumb.Sizes[thumb.Fit4096],
|
|
|
|
thumb.Sizes[thumb.Fit7680],
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:19:52 +02:00
|
|
|
// ImageFromThumb returns a cropped area from an existing thumbnail image.
|
|
|
|
func ImageFromThumb(thumbName string, area Area, size Size, cache bool) (img image.Image, err error) {
|
2021-09-05 17:10:52 +02:00
|
|
|
// Use same folder for caching if "cache" is true.
|
2021-09-05 21:19:52 +02:00
|
|
|
filePath := filepath.Dir(thumbName)
|
2021-09-05 17:10:52 +02:00
|
|
|
|
2021-09-05 19:10:44 +02:00
|
|
|
// Extract hash from file name.
|
2021-09-05 21:19:52 +02:00
|
|
|
hash := thumbHash(thumbName)
|
2021-09-05 17:10:52 +02:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Resolve symlinks.
|
|
|
|
if thumbName, err = fs.Resolve(thumbName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:10:52 +02:00
|
|
|
// Compose cached crop image file name.
|
2022-04-15 09:42:07 +02:00
|
|
|
cropBase := fmt.Sprintf("%s_%dx%d_crop_%s%s", hash, size.Width, size.Height, area.String(), fs.ExtJPEG)
|
2021-09-05 19:10:44 +02:00
|
|
|
cropName := filepath.Join(filePath, cropBase)
|
2021-09-05 17:10:52 +02:00
|
|
|
|
|
|
|
// Cached?
|
2021-09-05 19:10:44 +02:00
|
|
|
if !fs.FileExists(cropName) {
|
2021-09-05 17:10:52 +02:00
|
|
|
// Do nothing.
|
2021-09-05 19:10:44 +02:00
|
|
|
} else if img, err := imaging.Open(cropName); err != nil {
|
|
|
|
log.Errorf("crop: failed loading %s", filepath.Base(cropName))
|
2021-09-05 17:10:52 +02:00
|
|
|
} else {
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2021-09-05 19:10:44 +02:00
|
|
|
// Open thumb image file.
|
2021-09-05 21:19:52 +02:00
|
|
|
img, err = openIdealThumbFile(thumbName, hash, area, size)
|
2021-09-05 17:10:52 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get absolute crop coordinates and dimension.
|
|
|
|
min, max, dim := area.Bounds(img)
|
|
|
|
|
|
|
|
if dim < size.Width {
|
2021-09-05 21:19:52 +02:00
|
|
|
log.Debugf("crop: %s is too small, upscaling %dpx to %dpx", filepath.Base(thumbName), dim, size.Width)
|
2021-09-05 17:10:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Crop area from image.
|
|
|
|
img = imaging.Crop(img, image.Rect(min.X, min.Y, max.X, max.Y))
|
|
|
|
|
|
|
|
// Resample crop area.
|
|
|
|
img = thumb.Resample(img, size.Width, size.Height, size.Options...)
|
|
|
|
|
|
|
|
// Cache crop image?
|
|
|
|
if cache {
|
2021-09-05 19:10:44 +02:00
|
|
|
if err := imaging.Save(img, cropName); err != nil {
|
|
|
|
log.Errorf("crop: failed caching %s", filepath.Base(cropName))
|
2021-09-05 17:10:52 +02:00
|
|
|
} else {
|
2021-09-05 19:10:44 +02:00
|
|
|
log.Debugf("crop: saved %s", filepath.Base(cropName))
|
2021-09-05 17:10:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|
2021-09-05 19:10:44 +02:00
|
|
|
|
2021-09-05 21:19:52 +02:00
|
|
|
// ThumbFileName returns the ideal thumb file name.
|
|
|
|
func ThumbFileName(hash string, area Area, size Size, thumbPath string) (string, error) {
|
|
|
|
if len(hash) < 4 {
|
2022-04-15 09:42:07 +02:00
|
|
|
return "", fmt.Errorf("invalid file hash %s", clean.Log(hash))
|
2021-09-05 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(thumbPath) < 1 {
|
|
|
|
return "", fmt.Errorf("cache path missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if area.W <= 0 {
|
|
|
|
return "", fmt.Errorf("invalid area width %f", area.W)
|
|
|
|
}
|
|
|
|
|
|
|
|
if size.Width <= 0 {
|
|
|
|
return "", fmt.Errorf("invalid crop size %d", size.Width)
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := path.Join(thumbPath, hash[0:1], hash[1:2], hash[2:3])
|
|
|
|
fileName := findIdealThumbFileName(hash, area.FileWidth(size), filePath)
|
|
|
|
|
|
|
|
if fileName == "" {
|
|
|
|
return "", fmt.Errorf("not found")
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Resolve symlinks.
|
|
|
|
return fs.Resolve(fileName)
|
2021-09-05 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileWidth returns the minimal thumbnail width based on crop area and size.
|
|
|
|
func FileWidth(area Area, size Size) int {
|
|
|
|
return int(float32(size.Width) / area.W)
|
|
|
|
}
|
|
|
|
|
2021-09-05 19:10:44 +02:00
|
|
|
// thumbHash returns the thumb filename base without extension and size.
|
|
|
|
func thumbHash(fileName string) (base string) {
|
|
|
|
base = filepath.Base(fileName)
|
|
|
|
|
|
|
|
// Example: 01244519acf35c62a5fea7a5a7dcefdbec4fb2f5_1280x1024_fit.jpg
|
|
|
|
i := strings.Index(base, "_")
|
|
|
|
|
|
|
|
if i <= 0 {
|
|
|
|
return fs.StripExt(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
return base[:i]
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:19:52 +02:00
|
|
|
// findIdealThumbFileName finds the filename of the ideal thumb size for the given width.
|
|
|
|
func findIdealThumbFileName(hash string, width int, filePath string) (fileName string) {
|
|
|
|
if hash == "" || filePath == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2021-09-05 19:10:44 +02:00
|
|
|
|
|
|
|
for i, s := range thumbFileSizes {
|
2022-07-06 23:01:54 +02:00
|
|
|
// Resolve symlinks.
|
|
|
|
name, err := fs.Resolve(filepath.Join(filePath, fmt.Sprintf(thumbFileNames[i], hash)))
|
2021-09-05 19:10:44 +02:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
if err != nil || !fs.FileExists(name) {
|
2021-09-05 21:19:52 +02:00
|
|
|
continue
|
|
|
|
} else if s.Width < width {
|
|
|
|
fileName = name
|
|
|
|
continue
|
|
|
|
} else {
|
2021-09-05 19:10:44 +02:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
// openIdealThumbFile opens the thumbnail file and returns an image.
|
2022-07-06 23:01:54 +02:00
|
|
|
func openIdealThumbFile(fileName, hash string, area Area, size Size) (result image.Image, err error) {
|
|
|
|
// Resolve symlinks.
|
|
|
|
if fileName, err = fs.Resolve(fileName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-05 19:10:44 +02:00
|
|
|
if len(hash) != 40 || area.W <= 0 || size.Width <= 0 {
|
|
|
|
// Not a standard thumb name with sha1 hash prefix.
|
2021-10-06 07:10:50 +02:00
|
|
|
if imageBuffer, err := os.ReadFile(fileName); err != nil {
|
2021-09-05 19:10:44 +02:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return imaging.Decode(bytes.NewReader(imageBuffer), imaging.AutoOrientation(true))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:19:52 +02:00
|
|
|
if name := findIdealThumbFileName(hash, area.FileWidth(size), filepath.Dir(fileName)); name != "" {
|
|
|
|
fileName = name
|
|
|
|
}
|
2021-09-05 19:10:44 +02:00
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
if imageBuffer, err := os.ReadFile(fileName); err != nil {
|
2021-09-05 19:10:44 +02:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return imaging.Decode(bytes.NewReader(imageBuffer))
|
|
|
|
}
|
|
|
|
}
|