2020-01-06 17:50:05 +01:00
|
|
|
package thumb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2020-07-18 16:54:13 +02:00
|
|
|
"path/filepath"
|
2020-01-06 17:50:05 +01:00
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
2022-07-06 23:01:54 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-07-06 23:01:54 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-01-06 17:50:05 +01:00
|
|
|
)
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Jpeg converts an image to jpeg, saves and returns it.
|
2021-02-22 21:09:50 +01:00
|
|
|
func Jpeg(srcFilename, jpgFilename string, orientation int) (img image.Image, err error) {
|
2022-07-06 23:01:54 +02:00
|
|
|
// Resolve symlinks.
|
|
|
|
if srcFilename, err = fs.Resolve(srcFilename); err != nil {
|
|
|
|
log.Debugf("jpeg: %s in %s (resolve source image filename)", err, clean.Log(srcFilename))
|
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open source image.
|
2021-02-22 21:09:50 +01:00
|
|
|
img, err = imaging.Open(srcFilename)
|
2020-01-06 17:50:05 +01:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Failed?
|
2020-01-06 17:50:05 +01:00
|
|
|
if err != nil {
|
2022-07-06 23:01:54 +02:00
|
|
|
log.Errorf("jpeg: cannot open source image %s", clean.Log(filepath.Base(srcFilename)))
|
2020-05-01 11:53:41 +02:00
|
|
|
return img, err
|
2020-01-06 17:50:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Adjust orientation.
|
2021-02-22 21:09:50 +01:00
|
|
|
if orientation > 1 {
|
|
|
|
img = Rotate(img, orientation)
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Get JPEG quality setting.
|
2022-04-01 13:25:25 +02:00
|
|
|
quality := JpegQuality.EncodeOption()
|
2020-01-06 17:50:05 +01:00
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Save JPEG file.
|
2022-04-01 13:25:25 +02:00
|
|
|
if err = imaging.Save(img, jpgFilename, quality); err != nil {
|
2022-07-06 23:01:54 +02:00
|
|
|
log.Errorf("jpeg: failed to save %s", clean.Log(filepath.Base(jpgFilename)))
|
2020-05-01 11:53:41 +02:00
|
|
|
return img, err
|
2020-01-06 17:50:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Return JPEG image.
|
2020-05-01 11:53:41 +02:00
|
|
|
return img, nil
|
2020-01-06 17:50:05 +01:00
|
|
|
}
|