-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_processor_resize.go
56 lines (44 loc) · 1.09 KB
/
image_processor_resize.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
package imageprocessorimagick
import (
"io"
"io/ioutil"
"gopkg.in/gographics/imagick.v2/imagick"
"github.com/xaionaro-go/errors"
imageprocessorcommon "github.com/xaionaro-go/picapi/imageprocessor/common"
)
// Resize reads an image from `in` and writes a resized image to `out`
// (new image size: `toWidth`x`toHeight`).
//
// It returns image format and (if occured:) an error.
func (proc *ImageProcessor) Resize(
in io.Reader,
out io.Writer,
toWidth, toHeight uint,
) (imageFormat imageprocessorcommon.ImageFormat, err error) {
defer func() { err = errors.Wrap(err) }()
if toWidth <= 0 ||
toHeight <= 0 {
err = imageprocessorcommon.ErrInvalidSize
return
}
blob, err := ioutil.ReadAll(in)
if err != nil {
return
}
wand := imagick.NewMagickWand()
defer wand.Destroy()
err = wand.ReadImageBlob(blob)
if err != nil {
return
}
imageFormat = imageprocessorcommon.ParseImageFormat(wand.GetImageFormat())
err = wand.ResizeImage(toWidth, toHeight, imagick.FILTER_GAUSSIAN, 1)
if err != nil {
return
}
_, err = out.Write(wand.GetImageBlob())
if err != nil {
return
}
return
}