This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththumbnail.go
83 lines (72 loc) · 2.32 KB
/
thumbnail.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
package main
import (
"errors"
"github.com/mqp/lilliput"
)
type OutputBuffer struct {
buf []byte
ops *lilliput.ImageOps
}
var EncodeOptions = map[mimeType]map[int]int{
"image/gif": map[int]int{},
"image/jpeg": map[int]int{lilliput.JpegQuality: 85},
"image/png": map[int]int{lilliput.PngCompression: 7},
}
// Map from output media type to Lilliput output file type identifier.
var outputFileTypes = map[mimeType]string{
"image/gif": ".gif",
"image/jpeg": ".jpeg",
"image/png": ".png",
}
// Our in-world GIF search shows up to 25 GIF results at once,
// so it's nice if our pool retains that many buffers
var outputBufferPool = make(chan *OutputBuffer, 25)
func processImage(data []byte, outputFormat mimeType, width int, height int, t *timer) ([]byte, error) {
decoder, err := lilliput.NewDecoder(data)
if err != nil {
return nil, errors.New("Error initializing image decoder")
}
defer decoder.Close()
t.Check()
header, err := decoder.Header()
if err != nil {
return nil, errors.New("Error reading image header")
}
imgWidth := header.Width()
imgHeight := header.Height()
if imgWidth > conf.MaxDimension || imgHeight > conf.MaxDimension {
return nil, errors.New("Source image is too big")
}
t.Check()
// kind of hacky, but let's assume that the maximum output size is that of an
// uncompressed 24-bit RGBA image at the maximum dimension -- that should handle
// all JPEGs and PNGs, and there has to be some limit on how gigantic a GIF we
// are expected to process
maxOutputSize := conf.MaxDimension * conf.MaxDimension * 4
var outputBuffer *OutputBuffer
select {
case outputBuffer = <-outputBufferPool: // acquire from pool
default: // pool is empty, create one
outputBuffer = &OutputBuffer{
buf: make([]byte, maxOutputSize),
ops: lilliput.NewImageOps(conf.MaxDimension),
}
}
t.Check()
defer func() {
outputBuffer.ops.Clear()
select {
case outputBufferPool <- outputBuffer: // release into pool
default: // pool is full, throw out this one
}
}()
opts := &lilliput.ImageOptions{
FileType: outputFileTypes[outputFormat],
Width: width,
Height: height,
ResizeMethod: lilliput.ImageOpsFit,
NormalizeOrientation: true,
EncodeOptions: EncodeOptions[outputFormat],
}
return outputBuffer.ops.Transform(decoder, opts, outputBuffer.buf)
}