I have to receive an image via form-data http request and send it to file storage.
However, a problem arises in the process - if the image is in .heic format (for example, iPhones are shot in this format) and it is vertical (height > width), then the image is converted to horizontal, that is, it simply lies on its side. I haven't noticed this problem on other formats.
I read about the lib github.com/rwcarlsen/goexif/exif
, which allows you to get exif data, which also stores the orientation of the image.
Here is the function for getting a file from form-data of the fiber framework:
func ParseFileToBytes(c *fiber.Ctx, fileName string) ([]byte, error) {
if c.Method() != fiber.MethodPatch {
return nil, errlst.HttpErrInvalidRequest
}
formFile, err := c.FormFile(fileName)
if err != nil { ... }
file, err := formFile.Open()
if err != nil { ... }
defer file.Close()
fileData, err := io.ReadAll(file)
if err != nil { ... }
exifData, err := exif.Decode(bytes.NewReader(fileData))
if exifData == nil {
logger.Log.Info("exifData is nil")
} else {
logger.Log.Info("exifData is not nil")
}
return fileData, nil
}
But “exifData is nil”
is always displayed, so I can’t determine in any way what orientation the file was originally in order to adjust its further storage.
I will be glad for any help!
This lib helped me: https://github.com/evanoberholster/imagemeta
func rotateImage(imageData []byte, img image.Image) image.Image {
exifData, err := imagemeta.Decode(bytes.NewReader(imageData))
if err != nil {
logger.Log.Error("EXIF data is not available for this image", zap.Error(err))
} else {
orientation := exifData.Orientation.String()
logger.Log.Info("Image orientation is " + orientation)
if orientation == "Rotate 90 CW" {
img = imaging.Rotate270(img)
}
}
return img
}