-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.go
78 lines (67 loc) · 2.43 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/disintegration/imaging"
"github.com/rwcarlsen/goexif/exif"
)
func ExtractExif(filename string) (map[string]string, error) {
tagmap := make(map[string]string)
log.Printf("Anlyzing exif data for image %v", filename)
imgFileExif, errExifOpen := os.Open(filename)
defer imgFileExif.Close()
if errExifOpen != nil {
log.Printf("ERROR openExif %v", errExifOpen.Error())
return tagmap, errExifOpen
}
metaData, exifErrDecode := exif.Decode(imgFileExif)
if exifErrDecode != nil {
log.Printf("ERROR exifErrDecode %v", exifErrDecode.Error())
return tagmap, exifErrDecode
}
addTag(metaData, tagmap, exif.DateTimeOriginal)
addTag(metaData, tagmap, exif.PixelXDimension)
addTag(metaData, tagmap, exif.PixelYDimension)
addTag(metaData, tagmap, exif.LensModel)
addTag(metaData, tagmap, exif.FNumber)
addTag(metaData, tagmap, exif.ExposureTime)
addTag(metaData, tagmap, exif.ISOSpeedRatings)
return tagmap, nil
}
func addTag(meta *exif.Exif, tagmap map[string]string, field exif.FieldName) {
tagval, err := meta.Get(field)
if err != nil {
if config.Debug {
log.Printf("Error cannot get %s: %v", field, err)
}
return
}
tagmap[string(field)] = tagval.String()
}
// resized versions of the image and return array of temporary location
// this is hopefully more efficient in terms of memory management since we need to open image ony once
func ResizeImage(filename string, resizeModes map[string]int) map[string]string {
resizeReponse := make(map[string]string)
src, err := imaging.Open(filename)
if err != nil {
log.Printf("ERROR failed to open image to resize %s: %v", filename, err)
return resizeReponse
}
for resizeMode, size := range resizeModes {
// Resize the cropped image to width = xxxx px preserving the aspect ratio.
thumbnail := imaging.Resize(src, size, 0, imaging.Lanczos)
// Save the resulting image as JPEG.
extension := filepath.Ext(filename)
var thumbnailFile = fmt.Sprintf("%s_%d%s", (filename)[0:len(filename)-len(extension)], size, extension)
log.Printf("Convert %s to temporary thumbnail %s qual %d", filename, thumbnailFile, config.ResizeQuality)
err = imaging.Save(thumbnail, thumbnailFile, imaging.JPEGQuality(config.ResizeQuality))
if err != nil {
log.Printf("ERROR failed to create resize image %s: %v, skipping", thumbnailFile, err)
thumbnailFile = ""
}
resizeReponse[resizeMode] = thumbnailFile
}
return resizeReponse
}