-
Notifications
You must be signed in to change notification settings - Fork 18
/
imagick.go
61 lines (50 loc) · 1.14 KB
/
imagick.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
// +build imagick all
package main
import (
"fmt"
"os"
"time"
"gopkg.in/gographics/imagick.v2/imagick"
)
func init() {
imagick.Initialize()
// TODO send imagick.Terminate to main() for clean deconstruction
RegisterResizer("magickwand_box", resizeMagickWand)
}
func resizeMagickWand(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
var err error
mw := imagick.NewMagickWand()
defer mw.Destroy()
err = mw.ReadImage(origName)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
start := time.Now()
filter := imagick.FILTER_BOX
w := mw.GetImageWidth()
h := mw.GetImageHeight()
if w > h {
err = mw.ResizeImage(150, 100, filter, 1)
} else {
err = mw.ResizeImage(100, 150, filter, 1)
}
if err != nil {
fmt.Println(time.Since(start))
fmt.Println(err)
return 0, origFileStat.Size()
}
err = mw.SetImageCompressionQuality(95)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
err = mw.WriteImage(newName)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}