preprocess.go 723 B

1234567891011121314151617181920212223242526272829
  1. package images
  2. import (
  3. "bytes"
  4. "io"
  5. "path/filepath"
  6. "strings"
  7. )
  8. /*
  9. * Preprocess image files on client side.
  10. * 1. possibly adjust the orientation
  11. * 2. resize the image to a width or height limit
  12. * 3. remove the exif data
  13. * Call this function on any file uploaded to SeaweedFS
  14. *
  15. */
  16. func MaybePreprocessImage(filename string, data []byte, width, height int) (resized io.ReadSeeker, w int, h int) {
  17. ext := filepath.Ext(filename)
  18. ext = strings.ToLower(ext)
  19. switch ext {
  20. case ".png", ".gif":
  21. return Resized(ext, bytes.NewReader(data), width, height, "")
  22. case ".jpg", ".jpeg":
  23. data = FixJpgOrientation(data)
  24. return Resized(ext, bytes.NewReader(data), width, height, "")
  25. }
  26. return bytes.NewReader(data), 0, 0
  27. }