This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
125 lines (106 loc) · 3.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2017 - Sarjono Mukti Aji <me@simukti.net>
// Unless otherwise noted, this source code license is MIT-License
package image
import (
"os"
"path/filepath"
"strings"
"github.com/h2non/bimg"
)
// Params Supported request params for transformation
type Params struct {
Width int `json:"w,omitempty"`
Height int `json:"h,omitempty"`
Quality int `json:"q,omitempty"`
Blur int `json:"blur,omitempty"`
Flip bool `json:"flip,omitempty"`
Flop bool `json:"flop,omitempty"`
Colour bimg.Interpretation `json:"c,omitempty"`
Format bimg.ImageType `json:"fmt,omitempty"`
Fit string `json:"fit,omitempty"`
}
// Job Main job for image transformation
// Ccontain basic info for source file and requested params
type Job struct {
RequestHash string `json:"-"`
MasterDir string `json:"-"`
ResultDir string `json:"-"`
SourceURL string `json:"-"`
SourcePath string `json:"source_path"`
Params Params `json:"image_params,omitempty"`
}
// Process Do image processing from downloading to vips process via bimg
func (job *Job) Process() error {
download := DownloadJob{
SourceURL: job.SourceURL,
SourcePath: job.SourcePath,
TargetDir: job.MasterDir,
TargetFilename: strings.Replace(job.SourcePath, "/", "_", -1),
}
sourceFilepath := filepath.Join(download.TargetDir, download.TargetFilename)
if _, err := os.Stat(sourceFilepath); err != nil {
if err := download.Process(); err != nil {
return err
}
}
params := job.Params
options := bimg.Options{
Enlarge: false,
Force: false,
NoProfile: true,
NoAutoRotate: true,
Crop: false,
Embed: false,
Interlace: true,
Compression: 9,
// http://nickyguides.digital-digest.com/bilinear-vs-bicubic.htm
Interpolator: bimg.Bilinear,
Interpretation: params.Colour,
Type: params.Format,
Quality: params.Quality,
Flip: params.Flip,
Flop: params.Flop,
}
buffer, bufferErr := bimg.Read(sourceFilepath)
if bufferErr != nil {
return bufferErr
}
image := bimg.NewImage(buffer)
imageSize, imageError := image.Size()
if imageError != nil {
return imageError
}
if params.Colour == 0 {
// see vips <include/image.h>
options.Interpretation = bimg.InterpretationSRGB
}
if params.Width > imageSize.Width {
params.Width = imageSize.Width
}
if params.Height > imageSize.Height {
params.Height = imageSize.Height
}
if params.Width > 0 && params.Height == 0 {
options.Width = params.Width
} else if params.Height > 0 && params.Width == 0 {
options.Height = params.Height
} else {
options.Crop = true
options.Width = params.Width
options.Height = params.Height
}
if params.Blur > 0 {
options.GaussianBlur = bimg.GaussianBlur{
Sigma: float64(params.Blur),
}
}
resultFilepath := filepath.Join(job.ResultDir, job.RequestHash)
result, err := image.Process(options)
if err != nil {
return err
}
if err := bimg.Write(resultFilepath, result); err != nil {
return err
}
return nil
}