2014-07-05 01:03:48 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2014-07-06 01:01:17 +00:00
|
|
|
"strings"
|
2018-07-05 02:34:17 +00:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2014-07-05 01:03:48 +00: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-16 21:11:25 +00:00
|
|
|
* Call this function on any file uploaded to SeaweedFS
|
2014-07-06 01:01:17 +00:00
|
|
|
*
|
|
|
|
*/
|
2018-07-05 02:34:17 +00:00
|
|
|
func MaybePreprocessImage(filename string, data []byte, width, height int) (resized io.ReadSeeker, w int, h int) {
|
2014-07-05 01:03:48 +00:00
|
|
|
ext := filepath.Ext(filename)
|
2014-07-06 01:01:17 +00:00
|
|
|
ext = strings.ToLower(ext)
|
2014-07-05 01:03:48 +00:00
|
|
|
switch ext {
|
2017-05-28 17:25:40 +00:00
|
|
|
case ".png", ".gif":
|
2018-07-05 02:34:17 +00:00
|
|
|
return Resized(ext, bytes.NewReader(data), width, height, "")
|
2014-07-05 01:03:48 +00:00
|
|
|
case ".jpg", ".jpeg":
|
2014-07-06 01:01:17 +00:00
|
|
|
data = FixJpgOrientation(data)
|
2018-07-05 02:34:17 +00:00
|
|
|
return Resized(ext, bytes.NewReader(data), width, height, "")
|
2014-07-05 01:03:48 +00:00
|
|
|
}
|
2018-07-05 02:34:17 +00:00
|
|
|
return bytes.NewReader(data), 0, 0
|
2014-07-05 01:03:48 +00:00
|
|
|
}
|