/
magickcropper.go
91 lines (70 loc) · 2 KB
/
magickcropper.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
79
80
81
82
83
84
85
86
87
88
89
90
91
package lib
import (
"gopkg.in/gographics/imagick.v3/imagick"
"math"
)
const (
ShapeMaskCircle string = "circle"
ShapeMaskRoundrect string = "rounded"
ShapeDiamond string = "diamond"
)
type MagickCropper struct {
*imagick.MagickWand
}
func (mc MagickCropper) GetAspect() float32 {
return float32(mc.GetImageWidth()) / float32(mc.GetImageHeight())
}
func (mc MagickCropper) SmartCrop(w, h uint) error {
var err error
aspect := mc.GetAspect()
if w > h {
err = mc.ResizeImage(w, uint(float32(w)/aspect), imagick.FILTER_LANCZOS2_SHARP)
} else {
err = mc.ResizeImage(uint(float32(h)*aspect), h, imagick.FILTER_LANCZOS2_SHARP)
}
nw := int(mc.GetImageWidth())
nh := int(mc.GetImageHeight())
mc.CropImage(w, h, int(math.Abs(float64(nw-int(w)))/2), int(math.Abs(float64(nh-int(h)))/2))
return err
}
func (mc MagickCropper) ShapeImage(t string, param float64) error {
w, h := mc.GetImageWidth(), mc.GetImageHeight()
result := imagick.NewMagickWand()
canvas := imagick.NewDrawingWand()
pw := imagick.NewPixelWand()
pw.SetColor("none")
result.NewImage(w, h, pw)
pw.SetColor("white")
canvas.SetFillColor(pw)
defaultShape := func(cnv *imagick.DrawingWand, width uint, height uint) {
cnv.Rectangle(0, 0, float64(width), float64(height))
}
switch t {
case ShapeMaskCircle:
if w > h {
canvas.Circle(float64(w)/2, float64(h)/2, float64(w)/2, float64(h-1))
} else {
canvas.Circle(float64(w)/2, float64(h)/2, float64(w-1), float64(h)/2)
}
case ShapeMaskRoundrect:
if param != 0 {
canvas.RoundRectangle(0, 0, float64(w), float64(h), param, param)
} else {
defaultShape(canvas, w, h)
}
case ShapeDiamond:
canvas.Polygon([]imagick.PointInfo{
{float64(w) / 2, 0},
{float64(w), float64(h) / 2},
{float64(w) / 2, float64(h)},
{0, float64(h) / 2},
})
default:
defaultShape(canvas, w, h)
}
result.DrawImage(canvas)
result.CompositeImage(mc.MagickWand, imagick.COMPOSITE_OP_SRC_IN, true, 0, 0)
mc.MagickWand.Clear()
mc.MagickWand.AddImage(result)
return nil
}