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
|
|
|
)
|
|
|
|
|
2023-02-21 11:39:49 +01:00
|
|
|
// Jpeg converts an image to JPEG, saves it, and returns it.
|
|
|
|
func Jpeg(srcFile, jpgFile string, orientation int) (img image.Image, err error) {
|
2022-07-06 23:01:54 +02:00
|
|
|
// Resolve symlinks.
|
2023-02-21 11:39:49 +01:00
|
|
|
if srcFile, err = fs.Resolve(srcFile); err != nil {
|
|
|
|
log.Debugf("jpeg: %s in %s (resolve filename)", err, clean.Log(srcFile))
|
2022-07-06 23:01:54 +02:00
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open source image.
|
2023-02-21 11:39:49 +01:00
|
|
|
img, err = imaging.Open(srcFile)
|
2020-01-06 17:50:05 +01:00
|
|
|
|
2022-10-31 13:25:02 +01:00
|
|
|
// Failed?
|
2020-01-06 17:50:05 +01:00
|
|
|
if err != nil {
|
2023-02-21 11:39:49 +01:00
|
|
|
log.Debugf("jpeg: failed to open %s", clean.Log(filepath.Base(srcFile)))
|
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.
|
2023-02-21 11:39:49 +01:00
|
|
|
if err = imaging.Save(img, jpgFile, quality); err != nil {
|
|
|
|
log.Errorf("jpeg: failed to save %s", clean.Log(filepath.Base(jpgFile)))
|
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
|
|
|
}
|