2014-07-05 09:03:48 +08:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
2018-07-05 10:34:17 +08:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2018-07-23 16:16:17 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2014-07-05 09:03:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Preprocess image files on client side.
|
|
|
|
* 1. possibly adjust the orientation
|
|
|
|
* 2. resize the image to a width or height limit
|
|
|
|
* 3. remove the exif data
|
2015-04-17 05:11:25 +08:00
|
|
|
* Call this function on any file uploaded to SeaweedFS
|
2014-07-06 09:01:17 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-07-05 10:34:17 +08:00
|
|
|
func MaybePreprocessImage(filename string, data []byte, width, height int) (resized io.ReadSeeker, w int, h int) {
|
2014-07-05 09:03:48 +08:00
|
|
|
ext := filepath.Ext(filename)
|
2014-07-06 09:01:17 +08:00
|
|
|
ext = strings.ToLower(ext)
|
2014-07-05 09:03:48 +08:00
|
|
|
switch ext {
|
2017-05-29 01:25:40 +08:00
|
|
|
case ".png", ".gif":
|
2018-07-05 10:34:17 +08:00
|
|
|
return Resized(ext, bytes.NewReader(data), width, height, "")
|
2014-07-05 09:03:48 +08:00
|
|
|
case ".jpg", ".jpeg":
|
2014-07-06 09:01:17 +08:00
|
|
|
data = FixJpgOrientation(data)
|
2018-07-05 10:34:17 +08:00
|
|
|
return Resized(ext, bytes.NewReader(data), width, height, "")
|
2014-07-05 09:03:48 +08:00
|
|
|
}
|
2018-07-05 10:34:17 +08:00
|
|
|
return bytes.NewReader(data), 0, 0
|
2014-07-05 09:03:48 +08:00
|
|
|
}
|